1. 編譯
使用編譯器將代碼編譯成二進(jìn)制目標(biāo)文件(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. 鏈接
將二進(jìn)制目標(biāo)文件鏈接生成可執(zhí)行文件
gcc -o main main.o 或者 gcc main.o -o main
g++ -o main main.o 或者 g++ main.o -o main
3. 執(zhí)行
執(zhí)行可執(zhí)行文件
./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 ...
[]:表示可寫可不寫
<>:表示必選
{}:表示必須要在括號(hào)給出一個(gè)選擇
...:表示前述元素可以在命令行中多次重復(fù)
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預(yù)處理輸test.i文件
gcc -S test.i //將預(yù)處理輸出文件test.i匯編程test.s文件
gcc -c test.s //將匯編輸出文件test.s編譯輸出為test.o文件
gcc -o test test.o 或者 gcc test.o -o test //將編譯輸出文件test.o鏈接成可執(zhí)行文件
g++和gcc的區(qū)別
- g++無論是.c或.cpp都統(tǒng)一按照c++程序來編譯雳灾,但gcc會(huì)區(qū)分查吊。
- gcc編譯.cpp文件需要手動(dòng)調(diào)用鏈接的c++庫(kù)
gcc main.cpp -lstdc++