vault backup: 2022-09-27 10:15:11
Affected files: .obsidian/workspace 01. 個人/02. 專注Study/C++/GCC.md attachments/Pasted image 20220926212505.png attachments/Pasted image 20220926212522.png
This commit is contained in:
10
.obsidian/workspace
vendored
10
.obsidian/workspace
vendored
@@ -9,7 +9,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "markdown",
|
"type": "markdown",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "01. 個人/02. 專注Study/C++/GCC.md",
|
"file": "00. Inbox/00. TODO.md",
|
||||||
"mode": "source",
|
"mode": "source",
|
||||||
"source": true
|
"source": true
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "backlink",
|
"type": "backlink",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "01. 個人/02. 專注Study/C++/GCC.md",
|
"file": "00. Inbox/00. TODO.md",
|
||||||
"collapseAll": false,
|
"collapseAll": false,
|
||||||
"extraContext": false,
|
"extraContext": false,
|
||||||
"sortOrder": "alphabetical",
|
"sortOrder": "alphabetical",
|
||||||
@@ -86,7 +86,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "outline",
|
"type": "outline",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "01. 個人/02. 專注Study/C++/GCC.md"
|
"file": "00. Inbox/00. TODO.md"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,6 +116,7 @@
|
|||||||
},
|
},
|
||||||
"active": "828beb43bb437dd1",
|
"active": "828beb43bb437dd1",
|
||||||
"lastOpenFiles": [
|
"lastOpenFiles": [
|
||||||
|
"01. 個人/02. 專注Study/C++/GCC.md",
|
||||||
"00. Inbox/00. TODO.md",
|
"00. Inbox/00. TODO.md",
|
||||||
"03. 資料收集/讀書筆記/20201224 - 寫作是最好的自我投資.md",
|
"03. 資料收集/讀書筆記/20201224 - 寫作是最好的自我投資.md",
|
||||||
"03. 資料收集/名言佳句.md",
|
"03. 資料收集/名言佳句.md",
|
||||||
@@ -124,7 +125,6 @@
|
|||||||
"03. 資料收集/翻牆/V2Ray.md",
|
"03. 資料收集/翻牆/V2Ray.md",
|
||||||
"02. 工作/01. Logitech/20210412 - TestCam.md",
|
"02. 工作/01. Logitech/20210412 - TestCam.md",
|
||||||
"02. 工作/01. Logitech/20210428 - Sega.md",
|
"02. 工作/01. Logitech/20210428 - Sega.md",
|
||||||
"02. 工作/01. Logitech/20210512 - Kong.md",
|
"02. 工作/01. Logitech/20210512 - Kong.md"
|
||||||
"02. 工作/01. Logitech/20210716 - AutoStation.md"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -86,4 +86,189 @@ GCC在鏈接時優先使用動態庫,只有當動態庫不存在時才開始
|
|||||||
### 靜態庫鏈接時,搜索庫文件路徑的順序
|
### 靜態庫鏈接時,搜索庫文件路徑的順序
|
||||||
1. `ld`會去找GCC命令中的參數`-L`
|
1. `ld`會去找GCC命令中的參數`-L`
|
||||||
2. gcc的環境變量`LIBRARY_PATH`
|
2. gcc的環境變量`LIBRARY_PATH`
|
||||||
3. `/lib`,`/usr/lib`,`/usr/local/lib`等寫在程序內的路徑
|
3. `/lib`,`/usr/lib`,`/usr/local/lib`等寫在程序內的路徑
|
||||||
|
|
||||||
|
### 動態庫鏈接時,搜索庫文件路徑的順序
|
||||||
|
1. 編譯目標代碼時指定的動態庫搜索路徑
|
||||||
|
2. gcc的環境變量`LD_LIBRARY_PATH`
|
||||||
|
3. 配置文件`/etc/ld.so.conf`中指定的動態庫搜索路徑
|
||||||
|
4. 默認的動態庫搜索路徑`/lib`
|
||||||
|
5. 默認的動態庫搜索路徑`/usr/lib`
|
||||||
|
|
||||||
|
## 實用的工具
|
||||||
|
### ldd
|
||||||
|
列出依賴的動態庫
|
||||||
|
![[Pasted image 20220926212505.png]]
|
||||||
|
|
||||||
|
### nm
|
||||||
|
查看動態庫/靜態庫中的函數
|
||||||
|
![[Pasted image 20220926212522.png]]
|
||||||
|
|
||||||
|
## gcc/g++命令常見參數
|
||||||
|
命令格式
|
||||||
|
|
||||||
|
```bash
|
||||||
|
gcc [-c|-S|-E] [-std=standard]
|
||||||
|
[-g] [-pg] [-Olevel]
|
||||||
|
[-Wwarn...] [-pedantic]
|
||||||
|
[-Idir...] [-Ldir...]
|
||||||
|
[-Dmacro[=defn]...] [-Umacro]
|
||||||
|
[-foption...] [-mmachine-option...]
|
||||||
|
[-o outfile] [@file] infile...
|
||||||
|
```
|
||||||
|
|
||||||
|
在linux環境使用:`man g++`可以查看g++命令常用參數
|
||||||
|
|
||||||
|
### 常見參數如下(注意大小寫):
|
||||||
|
```bash
|
||||||
|
-o
|
||||||
|
#输出到指定文件。如果不指定,默认输出到a.out
|
||||||
|
|
||||||
|
-E
|
||||||
|
#仅进行预处理,不进行编译、汇编和链接
|
||||||
|
|
||||||
|
-S
|
||||||
|
#将代码转换为文件格式为xxx.s的汇编语言文件,但不进行汇编
|
||||||
|
|
||||||
|
-c
|
||||||
|
#仅进行编译和汇编,不进行链接操作,常用于编译不包含main程序的子程序代码
|
||||||
|
|
||||||
|
-v
|
||||||
|
#打印gcc编译时的详细步骤信息
|
||||||
|
复制代码
|
||||||
|
```
|
||||||
|
|
||||||
|
### 編譯和路徑參數
|
||||||
|
```bash
|
||||||
|
-l[basic library]
|
||||||
|
#编译时指定要使用的基础库,样例:-lpthread,针对Posix线程共享库进行编译
|
||||||
|
|
||||||
|
-L[shared-library path]
|
||||||
|
#共享库的路径添加到搜索的范围,路径为包含xxx.dll/xxx.so/xxx.dlyb文件的目录
|
||||||
|
|
||||||
|
-I[include header-file path]
|
||||||
|
#将头文件的路径添加到搜索的范围,路径为包含xxx.h/xxx.hpp文件的目录
|
||||||
|
|
||||||
|
-shared
|
||||||
|
#生成共享库,库文件格式为xxx.dll/xxx.so/xxx.dlyb格式的文件
|
||||||
|
|
||||||
|
-static
|
||||||
|
#生成静态库,库文件格式为xxx.a格式的文件
|
||||||
|
|
||||||
|
-Wl
|
||||||
|
#告诉编译器将后面的参数传递给链接器
|
||||||
|
|
||||||
|
-Wl,-Bstatic
|
||||||
|
#-Bstatic选项用于对指定的库静态连接
|
||||||
|
|
||||||
|
-Wl,-Bdynamic
|
||||||
|
#-Bdynamic搜索共享库(默认)
|
||||||
|
|
||||||
|
-Wa,option
|
||||||
|
#此选项传递option给汇编程序;如果option中间有逗号,就将option分成多个选项,然后传递给会汇编程序
|
||||||
|
|
||||||
|
-Wl,option
|
||||||
|
#此选项传递option给连接程序;如果option中间有逗号,就将option分成多个选项,然后传递给会连接程序
|
||||||
|
复制代码
|
||||||
|
```
|
||||||
|
|
||||||
|
### 預處理參數
|
||||||
|
```bash
|
||||||
|
#使用形式:-D[FLAG] 或-D[FLAG]=VALUE
|
||||||
|
|
||||||
|
-Dmacro
|
||||||
|
#在命令行里定义宏,相当于C语言中的"#define macro"
|
||||||
|
|
||||||
|
-Umacro
|
||||||
|
#相当于C语言中的"#undef macro"
|
||||||
|
|
||||||
|
-undef
|
||||||
|
#取消对任何非标准宏的定义
|
||||||
|
复制代码
|
||||||
|
```
|
||||||
|
|
||||||
|
### 警告與報錯參數
|
||||||
|
```bash
|
||||||
|
-Wall
|
||||||
|
#发出gcc提供的所有有用的报警信息
|
||||||
|
|
||||||
|
-Werror
|
||||||
|
#将警告升级为编译报错
|
||||||
|
|
||||||
|
|
||||||
|
-Wextra / -W
|
||||||
|
#启用-Wall未启用的额外警告位,对合法但值得怀疑的代码发出警告 例如 -Wsign-compare
|
||||||
|
|
||||||
|
|
||||||
|
-pendantic / -Wpendantic
|
||||||
|
#发出ISO C和ISO C++标准列出的所有警告,用于语法检查,-pedantic-erros的用法也类似
|
||||||
|
|
||||||
|
-fsyntax-only
|
||||||
|
#仅做语法检查
|
||||||
|
复制代码
|
||||||
|
```
|
||||||
|
|
||||||
|
### 調試參數
|
||||||
|
```bash
|
||||||
|
-g
|
||||||
|
#产生带有调试信息的目标代码
|
||||||
|
|
||||||
|
-gstabs
|
||||||
|
#此选项以stabs格式声称调试信息,但是不包括gdb调试信息
|
||||||
|
|
||||||
|
-gstabs+
|
||||||
|
#此选项以stabs格式声称调试信息,并且包含仅供gdb使用的额外调试信息
|
||||||
|
|
||||||
|
-ggdb
|
||||||
|
#生成gdb专用的调试信息
|
||||||
|
|
||||||
|
-glevel
|
||||||
|
#请求生成调试信息,同时用level指出需要多少信息,默认的level值是2
|
||||||
|
复制代码
|
||||||
|
```
|
||||||
|
|
||||||
|
### 編碼配置參數
|
||||||
|
```bash
|
||||||
|
-fno-exceptions
|
||||||
|
#屏蔽掉C++的异常,常用于于嵌入式或无法接受异常的系统
|
||||||
|
|
||||||
|
-fno-rtti
|
||||||
|
#禁用RTTI,常用于嵌入式或游戏开发
|
||||||
|
|
||||||
|
-fno-asm
|
||||||
|
#不要识别asm,inline或typeof作为关键字,以便代码可以使用这些词作为标识符。您可以使用关键字__asm__,__inline__来__typeof__ 代替。 -ansi暗示-fno-asm
|
||||||
|
|
||||||
|
-fPIC / -fpic
|
||||||
|
#让编译器的代码和位置无关,让代码逻辑不使用绝对地址,只用相对地址,方便文件加载
|
||||||
|
|
||||||
|
-nostdinc
|
||||||
|
#使编译器不再系统默认的头文件目录里面找头文件, 一般和 -I 联合使用,明确限定头文件的位置
|
||||||
|
|
||||||
|
-nostdin C++
|
||||||
|
#规定不在g++指定的标准路经中搜索,但仍在其他路径中搜索,.此选项在创建libg++库使用
|
||||||
|
复制代码
|
||||||
|
```
|
||||||
|
|
||||||
|
### 優化參數
|
||||||
|
```bash
|
||||||
|
-O0
|
||||||
|
#不优化
|
||||||
|
|
||||||
|
-O1 / -O
|
||||||
|
#尝试优化编译时间和可执行文件大小
|
||||||
|
|
||||||
|
-O2
|
||||||
|
#尝试所有的优化选项,但不会进行“空间换时间”的优化方式
|
||||||
|
|
||||||
|
-Os
|
||||||
|
#尝试所有的优化选项时,优先优化可执行文件大小
|
||||||
|
复制代码
|
||||||
|
```
|
||||||
|
|
||||||
|
## 來源
|
||||||
|
- [C/C++生態工具鏈——gcc/g++編譯器使用指南- 掘金](https://juejin.cn/post/7143280156042330125)
|
||||||
|
|
||||||
|
## 參考
|
||||||
|
- [Top (Using the GNU Compiler Collection (GCC))](https://gcc.gnu.org/onlinedocs/gcc/)
|
||||||
|
- [The C++ Compilation Model | C++ Fundamentals](https://subscription.packtpub.com/book/programming/9781789801491/1/ch01lvl1sec03/the-c-compilation-model)
|
||||||
|
- [What is LD_LIBRARY_PATH used for?](https://linuxhint.com/what-is-ld-library-path/)
|
||||||
BIN
attachments/Pasted image 20220926212505.png
Normal file
BIN
attachments/Pasted image 20220926212505.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 147 KiB |
BIN
attachments/Pasted image 20220926212522.png
Normal file
BIN
attachments/Pasted image 20220926212522.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
Reference in New Issue
Block a user