目錄
- 安裝
- 準備test.cpp
- 編譯
- g++ 編譯參數
- -g :編譯帶調試信息的可執行文件
- -O[n] :開啟優化
- -l 和 -L :指定庫文件 | 指定庫文件路徑
- -I :指定頭文件搜索目錄
- -Wall 和 -w:打印警告信息 | 關閉警告信息
- -std=c++11 :設置編譯標準
- -o :指定輸出文件名
- -D :定義宏
- 實例演示
- 直接編譯
- 生成庫文件并編譯
參考:
https://b23.tv/tWqKrC
我的linux版本如下:
Linux version 5.4.0-88-generic (buildd@lgw01-amd64-008) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04))
使用命令:
cat /proc/version
即可查看
安裝
sudo apt update
# 安裝編譯器和調試器
sudo apt install build-essential gdb
# 安裝完成后,確認是否成功
gcc --version
g++ --version
gdb --version
準備test.cpp
mkdir test
cd test
touch test.cpp
vim test.cpp
# 設置tab縮進
:set tabstop=4
然后寫代碼:
#include<iostream>
using namespace std;
int main()
{cout << "test" << endl;return 0;
}
編譯
在實際使用中,使用gcc指令編譯c代碼,g++指令編譯c++代碼。
一般cpp文件需要通過四個過程生成可執行文件:
預處理、編譯、匯編、鏈接
# 預處理 .cpp -> .i
# -E 表示編譯器只對輸入文件進行預處理
g++ -E test.cpp -o test.i
# 編譯 .i -> .s
# -S 表示在c++代碼產生了匯編語言文件后停止編譯,產生的匯編語言文件為 .s
g++ -S test.i -o test.s
# 匯編 .s -> .o
# -c g++僅把源代碼編譯為機器語言的目標代碼
g++ -c test.s -o test.o
# 鏈接 .o -> bin文件
# -o 為將來產生的可執行文件用指定的文件名
g++ test.o -o test
以上的步驟也可以直接用一個步驟代替:
g++ test.cpp -o test
現在test文件夾里面是這樣:
drwxrwxrwx 2 dyy dyy 4096 10月 19 14:44 ./
drwxrwxrwx 8 777 dyy 4096 10月 19 13:54 ../
-rwxrwxr-x 1 dyy dyy 17320 10月 19 14:44 test*
-rw-rw-r-- 1 dyy dyy 91 10月 19 14:38 test.cpp
運行它: 成功
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/test$ ./test
test
g++ 編譯參數
-g :編譯帶調試信息的可執行文件
# -g 選項告訴 GCC 產生能被 GNU 調試器GDB使用的調試信息,以調試程序。
g++ -g test.cpp
-O[n] :開啟優化
# -O 選項告訴 g++ 對源代碼進行基本優化。這些優化在大多數情況下都會使程序執行的更快。 -
# -O0 表示不做優化
# -O1 為默認優化
# -O2 除了完成-O1的優化之外,還進行一些額外的調整工作,如指令調整等。
# -O3 則包括循環展開和其他一些與處理特性相關的優化工作。
g++ -O2 test.cpp
-l 和 -L :指定庫文件 | 指定庫文件路徑
# -l參數(小寫)就是用來指定程序要鏈接的庫,-l參數緊接著就是庫名
# 在/lib和/usr/lib和/usr/local/lib里的庫直接用-l參數就能鏈接# 鏈接glog庫
g++ -lglog test.cpp# 如果庫文件沒放在上面三個目錄里,需要使用-L參數(大寫)指定庫文件所在目錄
# -L參數跟著的是庫文件所在的目錄名
# 鏈接mytest庫,libmytest.so在/home/bing/mytestlibfolder目錄下
g++ -L/home/bing/mytestlibfolder -lmytest test.cpp
-I :指定頭文件搜索目錄
# -I
# /usr/include目錄一般是不用指定的,gcc知道去那里找,但 是如果頭文件不在/usr/icnclude
# 里我們就要用-I參數指定了,比如頭文件放在/myinclude目錄里,那編譯命令行就要加上-
# I/myinclude 參數了,如果不加你會得到一個”xxxx.h: No such file or directory”的錯
# 誤。-I參數可以用相對路徑,比如頭文件在當前 目錄,可以用-I.來指定。上面我們提到的–cflags參
# 數就是用來生成-I參數的。
g++ -I/myinclude test.cpp
-Wall 和 -w:打印警告信息 | 關閉警告信息
# 打印出gcc提供的警告信息
g++ -Wall test.cpp
# 關閉警告信息
g++ -w test.cpp
-std=c++11 :設置編譯標準
# 使用c++11的標準編譯 test.cpp
g++ -std=c++11 test.cpp
-o :指定輸出文件名
# 若不指定,生成文件為a.out
g++ test.cpp -o test
-D :定義宏
# 常用場景:
# -DDEBUG 定義DEBUG宏,可能文件中有DEBUG宏部分的相關信息,用個DDEBUG來選擇開啟或關閉
# DEBUG
g++ -DDEBUG debugTest.cpp -o debugTest
比如:
#include<stdio.h>
using namespace std;
int main()
{#ifdef DEBUGprintf("DEBUG LOG\n");#endifprintf("in\n");
}
我們編譯運行:
g++ -DDEBUG debugTest.cpp -o debugTest
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/test$ ./debugTest
DEBUG LOG
in
實例演示
test項目錄如下:
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ tree
.
├── include
│ └── swap.h
├── main.cpp
└── src└── swap.cpp2 directories, 3 files
main.cpp內容:
#include<iostream>
#include<swap.h>
using namespace std;
int main()
{int val1 = 10;int val2 = 20;cout << "before" << endl;cout << val1 << " " << val2 << endl;swap(val1, val2);cout << "after" << endl;cout << val1 << " " << val2 <<endl;return 0;
}
swap.h內容
#ifndef _swap_h_
#define _swap_h_
#include<stdio.h>
void swap(int& a, int& b);
#endif
swap.cpp內容
#include"swap.h"
void swap(int& a, int& b)
{int tmp = a;a = b;b = tmp;
}
直接編譯
最簡單編譯運行
# 將 main.cpp src/Swap.cpp 編譯為可執行文件
g++ main.cpp src/Swap.cpp -Iinclude
# 運行可執行文件
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ ./a.out
before
10 20
after
20 10
增加編譯參數運行
g++ main.cpp src/swap.cpp -Iinclude -std=c++11 -O2 -Wall -o b.out
生成庫文件并編譯
首先嘗試生成靜態庫:
cd src
# 匯編,生成swap.o文件
g++ swap.cpp -c -I../include
# 生成靜態庫libswap.a
ar rs libswap.a swap.o
cd ..
# 鏈接 生成可執行文件staticmain
g++ main.cpp -Iinclude -Lsrc -lswap -o staticmain
現在觀察一下項目文件:
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ tree
.
├── include
│ └── swap.h
├── main.cpp
├── src
│ ├── libswap.a
│ ├── swap.cpp
│ └── swap.o
└── staticmain2 directories, 6 files
dyy@dyy-Lenovo-ThinkBo
現在嘗試動態鏈接
cd src
# 生成動態庫
g++ swap.cpp -I../include -fPIC -shared -o libswap.so
# 上面的命令等同于以下兩條合起來:
# g++ swap.cpp -I../include -c -fPIC
# g++ -shared -o libswap.so swap.o
cd ..
# 鏈接,生成可執行文件
g++ main.cpp -Iinclude -Lsrc -lswap -o sharemain
現在觀察一下項目文件:
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ tree
.
├── include
│ └── swap.h
├── main.cpp
├── sharemain
├── src
│ ├── libswap.a
│ ├── libswap.so
│ ├── swap.cpp
│ └── swap.o
└── staticmain
運行可執行文件
# 運行靜態鏈接的可執行文件
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ ./staticmain
before
10 20
after
20 10
# 運行含有動態鏈接的可執行文件
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ ./sharemain
./sharemain: error while loading shared libraries: libswap.so: cannot open shared object file: No such file or directory
# 指定一下動態庫目錄然后再運行
dyy@dyy-Lenovo-ThinkBook-14-IIL:~/Desktop/GCCTEST$ LD_LIBRARY_PATH=src ./sharemain
before
10 20
after
20 10