gcc/g++ compile options

Posted 2022-12-15 18:07:49 ‐ 1 min read

gcc

onenote 迁移文

option解释
-L指定链接库需要的目录 -L/usr/lib
-l链接库名称 -ltest. -Wl,-Bstatic 静态库 -Wl,-Bdynamic 动态库
-I头文件目录
-shared指定生成的动态库
-fPICshared library 独立位置代码,如果不用,每个使用 so 的 binary 会 copy 到自己进程空间一份,无法实现真正的 share library
gcc -E test.c -o test.i预处理
gcc -S test.i -o test.s编译
gcc -c test.s -o test.o汇编
gcc test.o -o test链接
-O编译优化 -O0, -O1(default), -O2, -O3
-g生成gdb 调试信息
-pggprof extra code
-Wall所有的警告信息 -W 很多选项
-Wl传递给 linker 的 option。-Wl,-R runtime lib 路径
-Wa传递给 assmbler 的 option

ar 工具可以将 object file 压缩到一起,ex. ar cqs libstaticlib.a one.o two.o three.o