bdonlan
bdonlan

Reputation: 231203

GCC switches to enable analysis for warnings

In GCC, certain warnings require optimization to be enabled. For example:

int foo() {
    int x;
    return x;
}

In order to detect the uninitialized variable, -O must be passed.

$ 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.

Upvotes: 2

Views: 1109

Answers (3)

Norman Ramsey
Norman Ramsey

Reputation: 202525

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.

Upvotes: 0

Bastien Léonard
Bastien Léonard

Reputation: 61723

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.)

-Wunused-variable
Warn whenever a local variable or non-constant static variable is unused aside from its declaration. This warning is enabled by -Wall.

3.8 Options to Request or Suppress Warnings

This behavior has changed in 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.

Upvotes: 2

JesperE
JesperE

Reputation: 64414

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.

Upvotes: 0

Related Questions