C++ Linux开发
GCC编译器
1.1 cpp编辑过程
- 预处理 Pre-Processing
宏替换,注释消除,查找相关库文件等
g++ -E test.cpp -o test.i
- 编译 Compling
将预处理后的文件转换成汇编语言,生成.s汇编文件
g++ -S test.i -o test.s
- 汇编 Assemnbling
将汇编文件转换为目标文件(机器代码),即.o文件
g++ -c test.s -o test.o
- 链接 Linking
链接相关目标文件或动静态库等,生成可执行文件
g++ test.o -o test
1.2 编译参数
- -o 指定输出文件名字, 不指定则输出为a.out
#指定文件名字为 myapp
g++ test.cpp -o myapp
- -g 编译带调试信息的可执行文件
g++ -g test.cpp -o test
- -O[n] 优化源代码,提高可执行文件的执行效率
g++ test.cpp -O2 -o test
- -I 指定头文件的搜索目录
g++ -I/My-Include-Floder/test test.cpp -o test
- -Wall 打印警告信息
g++ -Wall test.cpp -o test
- -w 关闭警告信息
g++ -w test.cpp -o test
- -std=c++11 设置编译标准
g++ -std=C++11 test.cpp -o test
- -l 指定库文件 -L 指定库文件路径
# -l 用来指定程序需要链接的库,-l参数后紧跟着的就是库名
# 在 /lib 和 /usr/lib 和 /usr/local/lib 中的库直接使用-l参数就可以连接到
# 如链接glog库
g++ -lglog test.cpp -o test
# 若库文件没有在上述三个目录,则需要使用-L指定路径
# 链接/home/mypyth 下的 glog 库,如下所示
g++ -L/home/mypath -lglog test.cpp -o test
- -D 定义宏
//使用场景, 以下是一段程序
#include<iostream>
using namespace std;
int main()
{
#ifdef MYDEBUG
cout<<"我被执行啦!";
#endif
cout<<"Hello C++"<<endl;
return 0;
}
#使用如下命令,输出结果为: 我被执行啦!Hello C++
g++ -DMYDEBUG test.cpp -o test
./test
#使用如下命令,输出结果为: Hello C++
g++ test.cpp -o test
./test
- 查看gcc帮助手册
man gcc