1. 编译
使用编译器将代码编译成二进制目标文件(object file)
gcc -c test1.c 或者gcc -c test1.c -o test1.o
g++ -c test1.cpp或者g++ -c test1.cpp -o test1.o
2. 链接
将二进制目标文件链接生成可执行文件
gcc -o main main.o 或者 gcc main.o -o main
g++ -o main main.o 或者 g++ main.o -o main
3. 执行
执行可执行文件
./main
4. 编译和链接
gcc -o main main.c test1.c
g++ -o main main.c test1.c
或者
gcc -c main.c test1.c
gcc -o main main.o test1.o
gcc命令
gcc [options] file ...
[]:表示可写可不写
<>:表示必选
{}:表示必须要在括号给出一个选择
...:表示前述元素可以在命令行中多次重复
options:
-c :complile and asemble, but do not link
-o <file> place the output into <file>
-wall:display the warning information
-E: preprocess only; do not assemble, compile or link
-S:compile only; do not assemble or link
gcc -E test.c test.i //将test.c预处理输test.i文件
gcc -S test.i //将预处理输出文件test.i汇编程test.s文件
gcc -c test.s //将汇编输出文件test.s编译输出为test.o文件
gcc -o test test.o 或者 gcc test.o -o test //将编译输出文件test.o链接成可执行文件
g++和gcc的区别
- g++无论是.c或.cpp都统一按照c++程序来编译,但gcc会区分。
- gcc编译.cpp文件需要手动调用链接的c++库
gcc main.cpp -lstdc++
网友评论