常用GCC编译选项
指定输出文件名
- -o file:编译完成的输出到file,否则会使用非常烂的默认名字(比如a.exe)。
指定宏定义
以下信息与手册上的写法略有出入[5],但是手册上的写法有时会报错。
- -Dname:等效于
#define name
。 - -Dname=definition:等效于
#define name definition
。
这样指定的宏定义在引用任何头文件前即存在。
包含头文件
- -Idir:将目录dir添加到搜索头文件的目录列表中[2]。
链接库
- -Ldir:将目录dir添加到链接库要搜索的目录列表中[2]。
- -llibrary:链接时搜索名为library的库[3]。对应的库文件名为liblibrary.a或liblibrary.so(Linux)。
注:-l选项要放在源文件列表后,同时依赖其他库的库要放在顺序靠前的位置,否则会出错。
比如,使用pthread库时要增加-lpthread参数,以链接libpthread.a。
查看帮助
运行
gcc --help
g++ --help
打印的信息极少,因此还是需要查阅手册。
参考
- 3.1 Option Summary (Using the GNU Compiler Collection (GCC));
- 3.16 Options for Directory Search (Using the GNU Compiler Collection (GCC));
- 3.15 Options for Linking (Using the GNU Compiler Collection (GCC));
- gcc/g++ 链接库的编译与链接 - CSDN博客;
- 3.13 Options Controlling the Preprocessor (Using the GNU Compiler Collection (GCC));