0%

GCC基本使用

The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, Go, and D, as well as libraries for these languages

基本命令格式

gcc(g++)

options

  • -o 指定文件夹
  • -fexec - charset = GBK 指定运行编码
  • -finput - charset = UTF - 8 指定源文件编码
  • -W 输出警告信息
  • -O(0-3) 指定代码优化级别
  • -E -o <filename.i> 编译过程进行到预处理
  • -S 编译过程到编译
  • -c 编译过程到汇编
  • -static 静态链接
  • -I 指定头文件搜索路径
  • -L 指定库文件搜索路径
  • -Wl, -rpath, 指定运行时动态链接搜索库(Linux有效) -Wl指定的是链接的选项,支持的选项用ld -help查看

静态链接库

  1. 命名 lib .a (具体库的名字)
  2. 生成 ar rcs libfun.a fun1.o fun.o (lib+静态链接库的名字)
  3. 使用 gcc main.c -L -lfun 或 gcc main.c /libfun.a (是路径)

动态链接库

  1. 命名 lib.dll/lib.so (在Windows上的后缀是.dll;在Linux上的后缀是.so)
  2. 生成 gcc -fPIC -shared -olibfun.dll fun.c (fPIC:位置无关的代码)

​ gcc -c -fPIC fun.c

​ gcc -o libfun.dll -shared fun.o

​ 加上 -Wl, –out - implib,libfun.dll.a

使用 gcc main.c -L

-lfun 或 gcc main.c /libfun.dll