GCC 常用选项
来源:互联网 发布:外贸出口的数据 编辑:程序博客网 时间:2024/06/04 19:36
-g: 生成调试信息
Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information.
某些时候,使用 -g 编译过后的执行程序中只是包括了源文件的名字,但没有路径名。此时,需要在 gdb 中通过 directory 指令执行源文件路径:
directory <dirname:dirname> 添加一个源文件路径到当前路径的前面
directory 清除所有的自定义源文件路径
show directories 显示定义了的源文件搜索路径
gdb 默认的 directory 是 $cdir 和 $pwd,分别表示:
$cdir directory embedded in executable recorded during compilation
$cwd current working dierctory
在本机上 compile 的 debug,不会出现找不到源代码的情况,就是因为 cdir。但是到了线上,一是线上无代码,二是即使拷贝了代码到线上,也必须放在与本机相同的目录(几乎不可能),所以,必须通过 directory 指定新的路径
-o: 指定目标文件
Place output in file file. If -o is not specified, the default is to put an executable file in a.out.
-c: 只编译不链接
Compile or assemble the source files, but do not link.
-llibrary: 链接到某个 so,例如 -lm 表示链接到函数库
Search the library named library when linking.
-Ldir: 查找库文件时,搜索该目录
Add directory dir to the list of directories to be searched for -l.
gcc 查找库文件时,搜索的目录及顺序为:1. -L 2. LIBRARY_PATH 3. lib 或 lib64、/usr/lib 或 /usr/lib64、/usr/local/lib 或 /usr/local/lib64
注意的是,以上仅仅是在编译时查找,不包括运行时 Load。
运行时搜索的目录及顺序为:1. /etc/ld.so.conf 2. LD_LIBRARY_PATH 3. -Wl -rpath 4. lib 或 lib64、/usr/lib 或 /usr/lib64、/usr/local/lib 或 /usr/local/lib64
在 Load 库文件时,如果我们生成的库不在标准目录,有好几种解决方法:
1. libxxx.so 拉到 /usr/lib 或 /lib 中去
2. export LD_LIBRARY_PATH=$(pwd) 对应的,编译时的环境变量为 LIBRARY_PATH
3. 在 /etc/ld.so.conf 文件里加入我们生成的库的目录,然后 /sbin/ldconfig。 /etc/ld.so.conf 是非常重要的一个目录,里面存放的是链接器和加载器搜索共享库时要检查的目录,默认是从 /usr/lib 和 /lib 中读取的,所以想要顺利运行,我们也可以把我们库的目录加入到这个文件中并执行 /sbin/ldconfig
具体请参考:http://blog.csdn.net/wellmikelan/article/details/8876757
关于 ldconfig:
ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib). The cache is used by the run-time linker, ld.so or ld-linux.so. ldconfig checks the header and filenames of the libraries it encounters when determining which versions should have their links updated.
即,修改了 /etc/ld.so.conf 之后,需要执行 ldconfig 重新建立索引。
PS:库文件相关都是 ld 开头,包括 ldconfig,LD_LIBRARY_PATH,/etc/ld.so.conf,是 load 的缩写吗?
另外还有个文件需要了解:/etc/ld.so.cache,里面保存了常用的动态函数库,且会先把他们加载到内存中,因为内存的访问速度远远大于硬盘的访问速度,这样可以提高软件加载动态函数库的速度。
-Idir: 查找头文件时,搜索该目录 为大写的 i 其实也很好记忆,头文件都需要 Include,首字母就是大写的 i
Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories. If the directory dir is a standard system include directory, the option is ignored to ensure that the default search order for system directories and the special treatment of system headers are not defeated .
gcc 查找头文件时,搜索的目录及顺序为:1. -I 2. C_INCLUDE_PATH CPLUS_INCLUDE_PATH OBJC_INCLUDE_PATH 3. /usr/include usr/local/include
-Dname: 宏定义
Predefine name as a macro, with definition 1.
-Dname=definition
The contents of definition are tokenized and processed as if they appeared during translation phase three in a `#define' directive.
-ansi: 这是最重要的标准选项,它告诉编译器遵守 C 语言的 ISO C90 标准。它关闭那些与标准不兼容的 gcc 扩展,禁用 C 语言程序中的 C++ 风格注释(//)。
-std=: 通过这个选项可以对使用的标准进行更精细的控制,默认使用 C89/C90,通过使用一个参数来设置需要的标准,主要选项如下:
c89
iso9899
gnu89
-Wall: 启用大多数 gcc 的告警选项,包括所有以 -W 为前缀的选项
Turns on all optional warnings which are desirable for normal code.
-pedantic: 启用用于检查代码是否遵循 C 语言标准的选项,还关闭了一些不被标准允许的传统 C 语言结构,并禁用所有的 GNU 扩展
Issue all the mandatory diagnostics listed in the C standard. Some of them are left out by default, since they trigger frequently on harmless code.
-Wformat: 检查 printf 系列函数所使用的参数类型是否正确
-Wparentheses: 检查是否总是提供了需要的圆括号,即使在某些环境中并不是必须要使用它们
-Wswitch-default: 检查是否所有的 switch 语句都包含一个 default case,这通常是一个好的编码习惯
-Wunused: 检查诸如声明静态函数但没有定义、未使用的参数和丢弃返回结果等情况
-Werror: 将 warning 视为 error,强制消除所有的 warning
-Wpadded: gcc 在发现结构体被填充时产生警告
test.c:5:19: 警告: 填补结构以对齐‘b’ [-Wpadded]
test.c:7:1: 警告: 将结构大小填补到对齐边界上 [-Wpadded]
-E: 查看宏定义展开之后的源码,分析 IOCCC 的代码时比较有效
Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. Input files that don't require preprocessing are ignored.
-fPIC: Position Independent Code,生成位置无关代码
在编译动态库(.so: shared object)时,如果需要链接某个静态库(.a),而该静态库在编译时未带上该选项,那么会出现形如下的提示:
/usr/bin/ld: /usr/lib/libpcre.a(pcre.o): relocation R_X86_64_32 against `a local symble` can not be used when making a shared object; recompile with -fPIC
/usr/lib/libpcre.a: could not read symbols: Bad value
解决办法时,带上该选项,重新编译这个静态库,然后再编译动态库
待补充:
-O:
此外,发现 GCC 的一个 bug:-lm 选项必须位于最后。该问题在 GCC 4.7.2 中存在。
- 【gcc】gcc常用选项
- gcc, g++ 常用选项
- gcc常用优化选项
- gcc 常用选项
- gcc常用选项小解
- gcc 几个常用选项
- 常用GCC 编译选项
- gcc常用选项
- 常用 GCC 编译选项
- GCC常用编译选项
- gcc 常用编译选项
- gcc常用控制选项
- gcc常用选项
- GCC 常用选项
- GCC常用选项
- gcc常用选项说明
- GCC常用编译选项
- gcc常用选项总结
- 51单片机学uCosII 跟我一起看书和实践
- ACDSee Pro 6.0.169 (x86) crack by XenoCoder
- 从事于电子商务,相忘于网络江湖!
- 根据两点经纬坐标计算两点间的距离[JAVA](转载)
- cookies可以跨域了~单点登陆(a.com.cn与b.com.cn可以共享cookies)
- GCC 常用选项
- 构造函数、复制构造函数、析构函数混合使用总结
- ACDSee Photo Manager 15.0.169 crack by XenoCoder
- MFC六大关键技术之(一)—— 初始化过程
- 实现两个变量的值传递和互换(不借助第3个变量)
- 那些年朋友劝,该知道的ASP.NET - 自定义控件处理页面事件(控件与页面数据交互)的方法
- SMSLib开发指南
- VC控件ListCtrl的使用方法总汇
- 解决jsp中传递参数汉字乱码问题