C语言 C 的有用 GCC 标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3375697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Useful GCC flags for C
提问by Matt Joiner
Beyond setting -Wall, and setting -std=XXX, what other really useful, but less known compiler flags are there for use in C?
除了 setting-Wall和 setting 之外-std=XXX,还有什么其他真正有用但鲜为人知的编译器标志可用于 C 中?
I'm particularly interested in any additional warnings, and/or and turning warnings into errors in some cases to absolutely minimize any accidental type mismatches.
我对任何额外的警告和/或在某些情况下将警告转化为错误特别感兴趣,以绝对减少任何意外的类型不匹配。
采纳答案by caf
Several of the -fcode generation options are interesting:
几个-f代码生成选项很有趣:
The
-ftrapvfunction will cause the program to abort on signed integer overflow (formally "undefined behaviour" in C).-fverbose-asmis useful if you're compiling with-Sto examine the assembly output - it adds some informative comments.-finstrument-functionsadds code to call user-supplied profiling functions at every function entry and exit point.
该
-ftrapv函数将导致程序在有符号整数溢出时中止(在 C 中正式为“未定义行为”)。-fverbose-asm如果您正在编译-S以检查程序集输出,则很有用- 它添加了一些信息性注释。-finstrument-functions添加代码以在每个函数入口和出口点调用用户提供的分析函数。
回答by Alok Singhal
Here are mine:
这是我的:
-Wextra,-Wall: essential.-Wfloat-equal: useful because usually testing floating-point numbers for equality is bad.-Wundef: warn if an uninitialized identifier is evaluated in an#ifdirective.-Wshadow: warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed.-Wpointer-arith: warn if anything depends upon the size of a function or ofvoid.-Wcast-align: warn whenever a pointer is cast such that the required alignment of the target is increased. For example, warn if achar *is cast to anint *on machines where integers can only be accessed at two- or four-byte boundaries.-Wstrict-prototypes: warn if a function is declared or defined without specifying the argument types.-Wstrict-overflow=5: warns about cases where the compiler optimizes based on the assumption that signed overflow does not occur. (The value 5 may be too strict, see the manual page.)-Wwrite-strings: give string constants the typeconst char[length]so that copying the address of one into a non-const char *pointer will get a warning.-Waggregate-return: warn if any functions that return structures or unions are defined or called.-Wcast-qual: warn whenever a pointer is cast to remove a type qualifier from the target type*.-Wswitch-default: warn whenever aswitchstatement does not have adefaultcase*.-Wswitch-enum: warn whenever aswitchstatement has an index of enumerated type and lacks acasefor one or more of the named codes of that enumeration*.-Wconversion: warn for implicit conversions that may alter a value*.-Wunreachable-code: warn if the compiler detects that code will never be executed*.
-Wextra,-Wall: 必不可少。-Wfloat-equal: 很有用,因为通常测试浮点数的相等性是不好的。-Wundef: 如果在#if指令中评估未初始化的标识符,则发出警告。-Wshadow: 每当一个局部变量遮蔽另一个局部变量、参数或全局变量时,或者每当一个内置函数被遮蔽时发出警告。-Wpointer-arith:警告是否有任何取决于函数或void.-Wcast-align: 每当指针被强制转换以增加目标所需的对齐时发出警告。例如,如果 achar *被强制转换为 aint *在只能在两字节或四字节边界访问整数的机器上,则发出警告。-Wstrict-prototypes: 如果在未指定参数类型的情况下声明或定义函数,则发出警告。-Wstrict-overflow=5: 警告编译器基于不发生有符号溢出的假设进行优化的情况。(值 5 可能过于严格,请参阅手册页。)-Wwrite-strings: 给字符串常量类型const char[长度,]以便将一个的地址复制到非const char *指针中会得到警告。-Waggregate-return: 如果任何返回结构或联合的函数被定义或调用,则发出警告。-Wcast-qual: 每当指针被强制转换以从目标类型* 中删除类型限定符时发出警告。-Wswitch-default: 每当switch语句没有defaultcase *时发出警告。-Wswitch-enum: 当switch语句具有枚举类型的索引并且缺少该枚举case的一个或多个命名代码时发出警告*。-Wconversion: 警告可能改变值* 的隐式转换。-Wunreachable-code: 如果编译器检测到代码永远不会被执行,则发出警告*。
Those marked *sometimes give too many spurious warnings, so I use them on as-needed basis.
那些标记为* 的有时会发出太多虚假警告,因此我会根据需要使用它们。
回答by Gilles 'SO- stop being evil'
Always use -Oor above (-O1, -O2, -Os, etc.). At the default optimization level, gcc goes for compilation speed and doesn't do enough analysis to warn about things like unitialized variables.
始终使用-O或上述(-O1,-O2,-Os等等)。在默认的优化级别,gcc 会提高编译速度,并且没有做足够的分析来警告诸如未初始化变量之类的事情。
Consider making -Werrorpolicy, as warnings that don't stop the compilation tend to be ignored.
考虑制定-Werror政策,因为不会停止编译的警告往往会被忽略。
-Wallpretty much turns on the warnings that are very likely to be errors.
-Wall几乎打开了很可能是错误的警告。
Warnings included in -Wextratend to flag common, legitimate code. They may be useful for code reviews (though lint-style programs find a lot more pitfalls are more flexible), but I wouldn't turn them on for normal development.
中包含的警告-Wextra往往会标记常见的合法代码。它们可能对代码很有用(尽管 lint 风格的程序发现更多的陷阱更灵活),但我不会为正常开发打开它们。
-Wfloat-equalis a good idea if the developers on the project are unfamiliar with floating point, and a bad idea if they are.
-Wfloat-equal如果项目的开发人员不熟悉浮点,这是一个好主意,如果他们不熟悉,则是一个坏主意。
-Winit-selfis useful; I wonder why it's not included in -Wuninitialized.
-Winit-self有用;我想知道为什么它不包含在-Wuninitialized.
-Wpointer-arithis useful if you have mostly-portable code that doesn't work with -pedantic.
-Wpointer-arith如果您的代码大多是可移植的,但不能与-pedantic.
回答by catphive
-save-temps
This leaves behind the results of the preprocessor and the assembly.
这留下了预处理器和程序集的结果。
The preprocessed source is useful for debugging macros.
预处理的源代码对于调试宏很有用。
The assembly is useful for determining what optimizations went into effect. For instance, you may want to verify that GCC is doing tail call optimization on some recursive functions, as without it you can potentially overflow the stack.
该程序集可用于确定哪些优化生效。例如,您可能想验证 GCC 是否正在对某些递归函数进行尾调用优化,因为没有它,您可能会溢出堆栈。
回答by Nordic Mainframe
-fmudflap-- adds runtime checks to all risky pointer operations to catch UB. This effectively immunizes your program again buffer overflows and helps to catch all kinds of dangling pointers.
-fmudflap-- 为所有危险的指针操作添加运行时检查以捕获 UB。这有效地再次免疫您的程序缓冲区溢出并有助于捕获各种悬空指针。
Here's a demo:
这是一个演示:
$ cat mf.c
int main()
{
int a[10];
a[10]=1; // <-- o noes, line 4
}
$ gcc -fmudflap mf.c -lmudflap
$ ./a.out
*******
mudflap violation 1 (check/write): time=1280862302.170759 ptr=0x7fff96eb3d00 size=44
pc=0x7f3a575503c1 location=`mf.c:4:2 (main)'
/usr/lib/libmudflap.so.0(__mf_check+0x41) [0x7f3a575503c1]
./a.out(main+0x90) [0x400a54]
/lib/libc.so.6(__libc_start_main+0xfd) [0x7f3a571e2c4d]
Nearby object 1: checked region begins 0B into and ends 4B after
mudflap object 0xf9c560: name=`mf.c:3:6 (main) a'
bounds=[0x7fff96eb3d00,0x7fff96eb3d27] size=40 area=stack check=0r/3w liveness=3
alloc time=1280862302.170749 pc=0x7f3a57550cb1
number of nearby objects: 1
回答by Nordic Mainframe
I'm surprised nobody has said this yet - the most useful flag as far as I'm concerned is -gwhich puts debugging information into the executable such that you can debug it and step through the source (unless you're proficient and reading assembly and like the stepicommand) of a program whilst it's executing.
我很惊讶还没有人这么说 - 就我而言,最有用的标志是-g将调试信息放入可执行文件中,以便您可以调试它并逐步执行源代码(除非您精通并阅读汇编和就像stepi程序执行时的命令一样。
回答by Amit Tomar
Not really related to C/C++, but useful anyways :
与 C/C++ 并不真正相关,但无论如何都很有用:
@file
Put all the above good flags (which you all have specified) in a 'file', and use this above flag to use all the flags in that file together.
将上述所有好的标志(你们都指定了)放在一个“文件”中,并使用上面的标志来一起使用该文件中的所有标志。
eg:
例如:
File : compilerFlags
文件:compilerFlags
-Wall
-std=c99
-Wextra
-墙
-std=c99
-Wextra
Then compile :
然后编译:
gcc yourSourceFile @compilerFlags
回答by sizzzzlerz
If you need to know the preprocessor flags that are predefined by the compiler:
如果您需要知道编译器预定义的预处理器标志:
echo | gcc -E -dM -
回答by Jens Gustedt
-march=nativeto produce optimized code for the platform (=chip) on which you are compiling
-march=native为您正在编译的平台(=芯片)生成优化的代码
回答by Michael Burr
It's not really helpful for detecting errors, but the rarely mentioned -masm=inteloption makes using -Sto inspect the assembly output much, much nicer.
它对检测错误并没有真正的帮助,但是很少提到的-masm=intel选项可以-S更好地检查程序集输出。
AT&T assembly syntax hurts my head far too much.
AT&T 汇编语法太让我头疼了。

