This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: compile with gcc option -O0 or -O


On 16/09/2011 09:54, Miles Bader wrote:
David Brown<david@westcontrol.com> writes:
but one weird problem is that the following codes compiled with option -O0

You need to show us a small complete self-contained example.


Normally a program which works at -O0 and fails at -O1 has an
uninitialized variable somewhere.

Another common cause is aliasing - using pointer casts to access the same data in different ways.

And (especially if this is embedded programming), also check for missing
"volatile" qualifiers.

Compile your code with lots of warnings - that will help spot
mistakes. The flags I often use are:

-Wall
-Wextra
-Winit-self
-Wmissing-include-dirs
...

... but be a bit wary of any warning option which isn't included in
-Wall or -Wextra -- they're usually omitted for a reason (typically
because they yield tons of false positives on reasonable code).


That depends on your definition of "reasonable code" ! I don't mean to say that the warnings /I/ use will suit everyone - or every type of programming. I often have to reduce them if I am working with other people's code, to avoid having lots of minor warning messages that can make real problems hard to spot.


An example here would be my insistence on "-Wmissing-declarations -Wnested-externs -Wmissing-prototypes -Wredundant-decls". When I write code, an object (variable, function, etc.) is either exported by a module, or it is local to a module. That means it is either declared "extern" in "module.h" and defined in "module.c" (which must #include "module.h"), or it is defined "static" in "module.c". And any external objects that are used must be accessed from #include'ing the other module's header file. I think these are good rules for the code I write (though I can see that some types of code need more flexibility), and these warnings help enforce them. But if you take typical old-fashioned (IMHO, of course) code then most objects are defined without "static" qualifiers or "extern" definitions, leading to lots of warnings.

And there are other warning flags that will be useful to other people, but not to me - "-pedantic" would be useful for people writing code that must work with different compilers, for example.

gcc has excellent static error checking capabilities - I make as much use of them as I can.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]