Gcc的常用编译选项


=Start=

缘由:

学习、提高需要

正文:

参考解答:

==

总体选项(Overall Option)

-c -S -E -o file -pipe -v -x language

语言选项(Language Option)

-ansi -fall-virtual -fcond-mismatch -fdollars-in-identifiers -fenum-int-equiv -fexternal-templates -fno-asm -fno-builtin -fhosted -fno-hosted -ffreestanding -fno-freestanding -fno-strict-prototype -fsigned-bitfields -fsigned-char -fthis-is-variable -funsigned-bitfields -funsigned-char -fwritable-strings -traditional -traditional-cpp -trigraphs

警告选项(Warning Option)

-fsyntax-only -pedantic -pedantic-errors -w -W -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscript -Wcomment -Wconversion -Wenum-clash -Werror -Wformat -Wid-clash-len -Wimplicit -Wimplicit-int -Wimplicit-function-declaration -Winline -Wlong-long -Wmain -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Wno-import -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wshadow -Wstrict-prototypes -Wswitch -Wtemplate-debugging -Wtraditional -Wtrigraphs -Wuninitialized -Wunused -Wwrite-strings

调试选项(Debugging Option)

-a -dletters -fpretend-float -g -glevel -gcoff -gxcoff -gxcoff+ -gdwarf -gdwarf+ -gstabs -gstabs+ -ggdb -p -pg -save-temps -print-file-name=library -print-libgcc-file-name -print-prog-name=program

优化选项(Optimization Option)

-fcaller-saves -fcse-follow-jumps -fcse-skip-blocks -fdelayed-branch -felide-constructors -fexpensive-optimizations -ffast-math -ffloat-store -fforce-addr -fforce-mem -finline-functions -fkeep-inline-functions -fmemoize-lookups -fno-default-inline -fno-defer-pop -fno-function-cse -fno-inline -fno-peephole -fomit-frame-pointer -frerun-cse-after-loop -fschedule-insns -fschedule-insns2 -fstrength-reduce -fthread-jumps -funroll-all-loops -funroll-loops -O -O2 -O3

预处理器选项(Preprocessor Option)

-Aassertion -C -dD -dM -dN -Dmacro[=defn] -E -H -idirafter dir -include file -imacros file -iprefix file -iwithprefix dir -M -MD -MM -MMD -nostdinc -P -Umacro -undef

汇编器选项(Assembler Option)

-Wa,option

连接器选项(Linker Option)

-llibrary -nostartfiles -nostdlib -static -shared -symbolic -Xlinker option -Wl,option -u symbol

目录选项(Directory Option)

-Bprefix -Idir -I- -Ldir

目标机选项(Target Option)

-b machine -V version

配置相关选项(Configuration Dependent Option)

代码生成选项(Code Generation Option)

-fcall-saved-reg -fcall-used-reg -ffixed-reg -finhibit-size-directive -fnonnull-objects -fno-common -fno-ident -fno-gnu-linker -fpcc-struct-return -fpic -fPIC -freg-struct-return -fshared-data -fshort-enums -fshort-double -fvolatile -fvolatile-global -fverbose-asm

==

一些常用选项摘录:

-llibrary
连接名为library的库文件.
连接器在标准搜索目录中寻找这个库文件,库文件的真正名字是`liblibrary.a'.连接器会 当做文件名得到准确说明一样引用这个文件.
搜索目录除了一些系统标准目录外,还包括用户以`-L'选项指定的路径.
一般说来用这个方法找到的文件是库文件---即由目标文件组成的归档文件(archive file).连接器处理归档文件的 方法是:扫描归档文件,寻找某些成员,这些成员的符号目前已被引用,不过还没有被定义.但是,如果连接器找到普通的 目标文件,而不是库文件,就把这个目标文件按平常方式连接进来.指定`-l'选项和指定文件名的唯一区别是, `-l选项用`lib'和`.a'把library包裹起来,而且搜索一些目录.

##
下列选项指定搜索路径,用于查找头文件,库文件,或编译器的某些成员:

-Idir
在头文件的搜索路径列表中添加dir 目录.

-I-
任何在`-I-'前面用`-I'选项指定的搜索路径只适用于`#include "file"'这种情况;他们不能用来搜索`#include <file>'包含的头文件.
如果用`-I'选项指定的搜索路径位于`-I-'选项后面,就可以在这些路径中搜索所有的 `#include'指令. (一般说来-I选项就是这么用的.)
还有, `-I-'选项能够阻止当前目录(存放当前输入文件的地方)成为搜索`#include "file"'的第一选择.没有办法克服`-I-'选项的这个效应.你可以指定 `-I.'搜索那个目录,它在调用编译器时是当前目录.这和预处理器的默认行为不完全一样,但是结果通常 令人满意.
`-I-'不影响使用系统标准目录,因此, `-I-'和`-nostdinc'是不同的选项.

-Ldir
在`-l'选项的搜索路径列表中添加dir目录.

-Bprefix
这个选项指出在何处寻找可执行文件,库文件,以及编译器自己的数据文件.
##

-Wformat
检查对printf和scanf等函数的调用,确认各个参数类型和格式串中的一致.

-Wall
使GCC产生尽可能多的警告信息.

-Werror
视警告为错误;出现任何警告即放弃编译.

-Dmacro
定义宏macro,宏的内容定义为字符串`1'.

-Dmacro=defn
定义宏macro的内容为defn.命令行上所有的`-D'选项在 `-U'选项之前处理.

-ansi
支持符合ANSI标准的C程序.
这样就会关闭GNU C中某些不兼容ANSI C的特性,例如asm, inline和 typeof关键字,以及诸如unix和vax这些表明当前系统类型的预定义宏.
使用`-ansi'选项不会自动拒绝编译非ANSI程序,除非增加`-pedantic'选项作为 `-ansi'选项的补充.

-pedantic
此编译选项并不能保证被编译程序与ANSI/ISO C标准的完全兼容,它仅仅只能用来帮助Linux程序员离这个目标越来越近。或者换句话说,-pedantic选项能够帮助程序员发现一些不符合 ANSI/ISO C标准的代码,但不是全部,事实上只有ANSI/ISO C语言标准中要求进行编译器诊断的那些情况,才有可能被GCC发现并提出警告。

-static
在支持动态连接(dynamic linking)的系统上,阻止连接共享库.该选项在其他系统上无效.

-shared
生成一个共享目标文件,他可以和其他目标文件连接产生可执行文件.只有部分系统支持该选项.

-g
以操作系统的本地格式(stabs, COFF, XCOFF,或DWARF).产生调试信息. GDB能够使用这些调试信息.

-ggdb
以本地格式(如果支持)输出调试信息,尽可能包括GDB扩展.

-p
产生额外代码,用于输出profile信息,供分析程序prof使用.

-pg
产生额外代码,用于输出profile信息,供分析程序gprof使用.

-fpic
如果支持这种目标机,编译器就生成位置无关目标码.适用于共享库(shared library).

-fPIC
如果支持这种目标机,编译器就输出位置无关目标码.适用于动态连接(dynamic linking),即使分支需要大范围 转移.

-v
(在标准错误)显示执行编译阶段的命令.同时显示编译器驱动程序,预处理器,编译器的版本号.
参考链接:

GNU GCC 手册
http://www.shanghai.ws/gnu/gcc_1.htm

GCC基本用法 #nice
http://www.woola.net/detail/2016-09-10-gcc-tips.html

GCC编译选项 #nice
http://txgcwm.github.io/blog/2014/06/28/gccbian-yi-xuan-xiang/

GCC Command Options
https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html

GCC警告选项例解
http://tonybai.com/2006/03/14/explain-gcc-warning-options-by-examples/

gcc编译器学习记录
https://github.com/guodongxiaren/LinuxTool/blob/master/gcc.md

[GCC/GDB/cygwin] gcc编译选项
http://www.airmyth.com/thread-1583-1-1.html

gcc的使用简介与命令行参数说明
http://blog.csdn.net/delphiwcdj/article/details/6555073

Gcc编译选项分析
http://www.cnblogs.com/showna/articles/1013401.html

GCC编译器总结
http://www.jianshu.com/p/b8ddb4cee7af

Linux GCC常用命令
http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html

Linux gcc编译参数 —- Werror的威力
http://www.leoox.com/?p=237

怎样学习GCC编译选项?
https://www.zhihu.com/question/28914753

=END=

, ,

《“Gcc的常用编译选项”》 有 7 条评论

  1. Find libraries a binary was linked against
    https://superuser.com/questions/239590/find-libraries-a-binary-was-linked-against

    在Linux下,如何强制让GCC静态链接?
    https://www.zhihu.com/question/22940048/answer/222625910

    Linux编译安装openssl升级系统openssl和生成系统动态连接库dynamic/shared libraries
    https://www.zhangfangzhou.cn/update-openssl-and-dynamicshared-libr.html

    Linking OpenSSL libraries to a program
    https://stackoverflow.com/questions/4352573/linking-openssl-libraries-to-a-program

    How to Statically Link C and C++ Programs on Linux with gcc
    https://www.systutorials.com/5217/how-to-statically-link-c-and-c-programs-on-linux-with-gcc/

    Static link of shared library function in gcc
    https://stackoverflow.com/questions/725472/static-link-of-shared-library-function-in-gcc

    http://www.unix.com/programming/162567-linking-openssl-libcrypto-statically.html

    https://askubuntu.com/questions/454443/how-do-i-deal-with-undefined-reference-to-dlopen-errors-while-compiling-and-us

    How to link a library statically?
    http://hgesser.de/howto/static-link.html

    https://stackoverflow.com/questions/tagged/glibc

    `
    1.首先要有静态库XYZ的文件(libXYZ.a);
    2.其次要在gcc链接选项中指定;
    3.最后使用ldd/readelf -d等命令测试一下;
    `

  2. […] 之前有记录过「静态库和动态库」、「GCC的常用编译选项」两篇文章,但说来惭愧,还一直没有真正实践过特定场景的静态编译,趁着周末有时间,再加上之前刚好有一个场景需要用到静态编译的功能,实际验证了一下Linux下gcc的静态编译的相关选项及其功能和注意事项。在此记录一下,方便以后参考。 […]

  3. GCC 7.3 发布,这个版本新增了用于缓解“幽灵” Spectre CPU 漏洞的编译选项(CVE 2017-5715)
    https://gcc.gnu.org/ml/gcc/2018-01/msg00197.html
    https://www.solidot.org/story?sid=55353
    `
    GCC 编译器项目释出了 7.3 版,新版本主要是修正 bug,包含了大量性能和安全问题的修正,其中最重要的是对 Retpoline 的支持,让 GCC 能用于构建抵抗 Spectre variant 2 漏洞的内核。Retpoline 是 Google 开发的补丁,能有效缓解 Spectre V2 漏洞,而且对性能影响甚微。它在 4.15-rc8 中合并到内核主线,但使用它需要编辑器支持,而在 GCC 7.3 之前还没有编译器支持 Retpoline。
    `

回复 a-z 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注