In gcc, certain warnings require optimization to be enabled. For example:
在gcc中,某些警告需要启用优化。例如:
int foo() {
int x;
return x;
}
In order to detect the uninitialized variable, -O must be passed.
为了检测未初始化的变量,必须传递-O。
$ gcc -W -Wall -c test.c
$ gcc -W -Wall -c test.c -O
test.c: In function ‘foo’:
test.c:3: warning: ‘x’ is used uninitialized in this function
However, this can interfere with debugging. Is there a way to enable just the analysis phases needed for warnings (and not just this particular warning, but as many as possible), without affecting the generated code too much?
但是,这可能会干扰调试。有没有办法只启用警告所需的分析阶段(而不仅仅是这个特定警告,但尽可能多),而不会过多地影响生成的代码?
I'm using gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4) on x86-64.
我在x86-64上使用gcc版本4.3.3(Ubuntu 4.3.3-5ubuntu4)。
3 个解决方案
#1
Try using -Wall
instead of -W
. -W
is deprecated IIRC. (As Jonathan Leffler points out in a comment, -W
's replacement is -Wextra
, not -Wall
.)
尝试使用-Wall而不是-W。 -W已弃用IIRC。 (正如Jonathan Leffler在评论中指出的那样,-W的替代品是-Wextra,而不是-Wall。)
[Edit]
-Wunused-variable
Warn whenever a local variable or non-constant static variable is unused aside from its declaration. This warning is enabled by-Wall
.-Wunused-variable当局部变量或非常量静态变量在其声明之外未使用时发出警告。 -Wall启用此警告。
http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Warning-Options.html#Warning-Options
[Edit]
This behavior has changed in GCC 4.4:
GCC 4.4中的此行为已更改:
Uninitialized warnings do not require enabling optimization anymore, that is,
-Wuninitialized
can be used together with-O0
. Nonetheless, the warnings given by-Wuninitialized
will probably be more accurate if optimization is enabled.未初始化的警告不再需要启用优化,即-Wuninitialized可以与-O0一起使用。尽管如此,如果启用优化,则-ununitialized给出的警告可能会更准确。
#2
DDD and gdb
can mostly cope with code compiled with gcc -O -g
. Sometimes variables aren't in scope when you expect them to be, but DDD is clever enough to say "optimized away" instead of freaking out. But there's no question it's easier to debug with -O turned off—I have seen this a lot with my students' code.
DDD和gdb主要可以处理用gcc -O -g编译的代码。有时变量不在你预期的范围内,但是DDD足够聪明地说“优化了”而不是吓坏了。但毫无疑问,关闭-O调试更容易 - 我的学生代码已经看到了很多。
#3
This is what you have your automated build for. Let your automated build engine build with -Werror -Wall -O2, and you'll catch all the warnings triggered by higher optimization levels.
这就是您自动构建的内容。让您的自动构建引擎使用-Werror -Wall -O2构建,并且您将捕获由更高优化级别触发的所有警告。
#1
Try using -Wall
instead of -W
. -W
is deprecated IIRC. (As Jonathan Leffler points out in a comment, -W
's replacement is -Wextra
, not -Wall
.)
尝试使用-Wall而不是-W。 -W已弃用IIRC。 (正如Jonathan Leffler在评论中指出的那样,-W的替代品是-Wextra,而不是-Wall。)
[Edit]
-Wunused-variable
Warn whenever a local variable or non-constant static variable is unused aside from its declaration. This warning is enabled by-Wall
.-Wunused-variable当局部变量或非常量静态变量在其声明之外未使用时发出警告。 -Wall启用此警告。
http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Warning-Options.html#Warning-Options
[Edit]
This behavior has changed in GCC 4.4:
GCC 4.4中的此行为已更改:
Uninitialized warnings do not require enabling optimization anymore, that is,
-Wuninitialized
can be used together with-O0
. Nonetheless, the warnings given by-Wuninitialized
will probably be more accurate if optimization is enabled.未初始化的警告不再需要启用优化,即-Wuninitialized可以与-O0一起使用。尽管如此,如果启用优化,则-ununitialized给出的警告可能会更准确。
#2
DDD and gdb
can mostly cope with code compiled with gcc -O -g
. Sometimes variables aren't in scope when you expect them to be, but DDD is clever enough to say "optimized away" instead of freaking out. But there's no question it's easier to debug with -O turned off—I have seen this a lot with my students' code.
DDD和gdb主要可以处理用gcc -O -g编译的代码。有时变量不在你预期的范围内,但是DDD足够聪明地说“优化了”而不是吓坏了。但毫无疑问,关闭-O调试更容易 - 我的学生代码已经看到了很多。
#3
This is what you have your automated build for. Let your automated build engine build with -Werror -Wall -O2, and you'll catch all the warnings triggered by higher optimization levels.
这就是您自动构建的内容。让您的自动构建引擎使用-Werror -Wall -O2构建,并且您将捕获由更高优化级别触发的所有警告。