系统:Ubuntu
编译器:g++
执行流程如下图:
步骤:
1)创建文件,后缀名设置为.cpp
;
touch test.cpp
2)使用vim编写C++程序;
#include <iostream>
using namespace std;
#define max 1024
int main(){
cout<<max<<endl;
cout<<"hello,world"<<endl;
return 0;
}
3)执行预编译,生成.i
文件;
g++ -E test.cpp -o test.i
//-E : 表示预编译
//-o : 表示输出文件
如果上面的语句执行没有出现其他输出,表示没有问题。
4)执行编译,生成.s
文件;
g++ -S test.i -o test.s
//-S : 表示编译
//-o : 表示输出文件
5)执行汇编,生成.o
文件;
g++ -c test.s -o test.o
//-c : 表示汇编
//-o : 表示输出文件
6)链接,生成可执行文件;
g++ test.s -o test
//注意 :
//1)这里链接的是test.s文件;
//2)这里的输出文件在linux中,可以不写后缀名,因为在linux中一切皆文件。
7)查看文件是否可执行,file
命令;
file test
如下图:
如果显示exectuable
,则表示文件是可执行的,说明前面的操作都是正确的。
8)运行c++程序,命令:./test
;
********************************************************************************************
前面是为了了解c++程序编译机制,实际中使用g++时,可以执行下面一句就可以了:
g++ test.cpp -o test
//注意 : 如果不需要自定义输出的文件名,那么可以直接写:
g++ test.cpp
//这样会生成一个默认的a.out可执行文件。