NotePublic/Software/Applications/GCC/GCC_编译连接参数.md

149 lines
4.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# GCC 编译连接参数
## 1.编译参数
### 1.1.编译警告控制参数
#### 1.1.1.Werror
-Werror将所有的警告当成错误进行处理。
#### 1.1.2.Wall
-Wall打开 gcc 的所有警告,主要包括以下部分:
```blk
-Waddress
-Warray-bounds=1 (only with -O2)
-Wbool-compare
-Wbool-operation
-Wc++11-compat-Wc++14-compat
-Wcatch-value (C++ and Objective-C++ only)
-Wchar-subscripts警告如果数组下标有char类型。 这是错误的常见原因,因为程序员经常忘记这种类型是在某些机器上签名的。
-Wcomment
-Wduplicate-decl-specifier (C and Objective-C only)
-Wenum-compare (in C/ObjC; this is on by default in C++)
-Wformat
-Wformat-security开启类型不匹配警告。
-Wint-in-bool-context
-Wimplicit (C and Objective-C only)
-Wimplicit-int (C and Objective-C only)
-Wimplicit-function-declaration (C and Objective-C only)
-Winit-self (only for C++)
-Wlogical-not-parentheses
-Wmain (only for C/ObjC and unless -ffreestanding)
-Wmaybe-uninitialized
-Wmemset-elt-size
-Wmemset-transposed-args
-Wmisleading-indentation (only for C/C++)
-Wmissing-attributes
-Wmissing-braces (only for C/ObjC)
-Wmultistatement-macros
-Wnarrowing (only for C++)
-Wnonnull
-Wnonnull-compare
-Wopenmp-simd
-Wparentheses
-Wpointer-sign
-Wreorder
-Wrestrict
-Wreturn-type
-Wsequence-point
-Wsizeof-pointer-div
-Wsizeof-pointer-memaccess
-Wstrict-aliasing
-Wstrict-overflow=1
-Wswitch
-Wtautological-compare
-Wtrigraphs
-Wuninitialized
-Wunknown-pragmas
-Wunused-function开启未使用的函数警告。
-Wunused-label
-Wunused-value
-Wunused-variable
-Wvolatile-register-var
```
每个警告参数都有对应的“-Wno-”参数实现相反的控制,例如:
```blk
-Wno-unused-parameter关闭未使用的参数警告。
-Wno-unused-function关闭未使用的函数警告。
-Wno-format-security关闭类型不匹配警告。
-Wno-sign-compare关闭有符号数和无符号数比较警告。
```
在同时开启 -Wall 和 -Werror 时将使编译警告变为编译错误,此时语法检查非常严格,如需放开某些限制,可结合“-Wno-”类参数使用,例如:-Wall -Werror -Wno-unused-parameter -Wno-unused-function。
#### 1.1.3.Wextra
-Wextra启用一些未由-Wall启用的额外警告标志。 (此选项过去称为-W ,旧名称仍然受支持,但更新的名称更具描述性。)
```blk
-Wclobbered
-Wcast-function-type
-Wempty-body
-Wignored-qualifiers
-Wimplicit-fallthrough=3
-Wmissing-field-initializers
-Wmissing-parameter-type (C only)
-Wold-style-declaration (C only)
-Woverride-init
-Wsign-compare (C only):开启有符号数和无符号数比较警告。
-Wtype-limits
-Wuninitialized
-Wshift-negative-value (in C++03 and in C99 and newer)
-Wunused-parameter (only with -Wunused or -Wall):开启未使用的参数警告。
-Wunused-but-set-parameter (only with -Wunused or -Wall)
```
选项 -Wextra 还会打印以下情况的警告消息:
指针与整数零与< <= >或>= 。
仅限C ++)枚举器和非枚举器都出现在条件表达式中。
仅限C ++)不明确的虚拟基础。
仅限C ++为已声明为register的数组下标。
仅限C ++取得已声明register的变量的地址。
仅限C ++)基类不在派生类的复制构造函数中初始化。
## 2.连接参数
### 2.1.rpath/rpath-link
如果 prog 依赖 libfoobar.so而 libfoobar.so libfoo.so 和 libbar.so按如下方式编译 program 将产生编译错误:
```bash
$ gcc -c -Wall main.c
$ gcc -o prog main.o -L. -lfoobar
/usr/bin/ld: warning: libfoo.so, needed by ./libfoobar.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libbar.so, needed by ./libfoobar.so, not found (try using -rpath or -rpath-link)
./libfoobar.so: undefined reference to `bar'
./libfoobar.so: undefined reference to `foo'
collect2: error: ld returned 1 exit status
```
此时可在编译 prog 时增加 -rpath/-rpath-link 参数:
```bash
gcc -o prog main.o -L. -lfoobar -Wl,-rpath=$(pwd)
```
即可。此时能够正常编译和运行 prog因为相关信息已经被写入到 libfoobar.so 文件中:
```bash
$ readelf -d prog
Dynamic section at offset 0xe08 contains 26 entries:
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [libfoobar.so]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000f (RPATH) Library rpath: [/home/imk/develop/so/scrap]
... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
```
## 3.外部参考资料
1. [gcc警告选项汇总](https://blog.csdn.net/qq_17308321/article/details/79979514)