Linux系统使用入门进阶总结(6)——Ubuntu下gcc/g++编译链接过程

文章转自:
https://blog.csdn.net/VennyJin/article/details/82794331
这里讲的是最简单的c/c++文件在linux下编译链接的过程,后期还可以使用cmake来完成更复杂的工程构建过程。请关注博主的后续文章哈~~~

Ubuntu下gcc / g++编译链接过程

1. 编辑源文件

假设我现在有一个最简单的C++文件:

1 #include <iostream>
2 using namespace std;
3 int main() {
4     cout << "Hello, world!!!!" << endl;
5     return 0;
6 }

2. 编译hello.cpp

[xxx@xxx ~]$ g++ -c hello.cpp

输出结果是一个hello.o文件,这是编译过程的生成的中间文件。-c 表示只编译,不链接

3. 链接hello.o生成hello.out

[xxx@xxx ~]$ g++ -o hello.out hello.o

输出结果是一个hello.out文件,这是最终的可执行文件。-o 表示输出文件,hello.o是上一步生成的.o文件。

当然,如果第2、3步是可以合并执行,直接执行命令

[xxx@xxx ~]$ g++ -o hello.out hello.cpp

然而第2、3步分开执行是有意义的,后面会讲到。

4. 运行hello.out

最后执行以下hello.out验证一下输出结果呗

[xxx@xxx ~]$ ./hello.out

猜你喜欢

转载自blog.csdn.net/VennyJin/article/details/82794331
今日推荐