

The most useful GCC options and extensions - antoarts
http://www.antoarts.com/the-most-useful-gcc-options-and-extensions/

======
gaius
_"If Java had true garbage collection, most programs would delete themselves
upon execution." — Robert Sewell_

Nice ;-)

~~~
rat
How is that nice? And I can't find this quote in the article.

~~~
gaius
It's in the sidebar. Just amused me is all.

~~~
antoarts
The quotes are randomly chosen from a database of my favorite ~50 quotes, so
it isn't related to the article, though

------
tedunangst
I was definitely not expecting an article about useful GCC options to mention
-trigraphs. The -ansi option isn't particularly useful either, imo, since just
about every interesting program is going to do something crazy like open a
network socket or list the files in a directory.

By far the most useful gcc extension wasn't mentioned at all. typeof().

~~~
antoarts
I mentioned trigraphs as an example of a C standard feature disabled by
default in GCC, and then I just mentioned how to enable just that feature. It
wasn't supposed to be included among the most important options, it just got
there ;-).

------
pdovy
Has anybody used the __builtin_expect feature to good effect? We had been
using this extensively in our codebase in places where there is an obvious
common "fast path".

I tried an experiment where I did a comparison of the __builtin_expect version
against a version with our likely/unlikely macros defined to the identity, and
I actually saw no difference, which was disappointing. When I did some digging
I found this blog post: [http://bitsup.blogspot.com/2008/04/measuring-
performance-of-...](http://bitsup.blogspot.com/2008/04/measuring-performance-
of-linux-kernel.html) which suggests the same result for the Linux kernel.

FWIW GCC also has a reasonable set of PGO options (not mentioned in this
article), and those actually can (anecdotally) make a noticeable difference,
although you're subject to the overtraining problem if you're not careful.

------
ryanpetrich
Another useful GCC extension is statements within expressions:

#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a :
_b; })

------
defen
Computed goto: [http://gcc.gnu.org/onlinedocs/gcc-3.4.1/gcc/Labels-as-
Values...](http://gcc.gnu.org/onlinedocs/gcc-3.4.1/gcc/Labels-as-Values.html)

------
matthavener
A few others I use are -Wsign-compare and -Wformat-nonliteral. Sign compare
warns on possible bugs/vulnerabilities due to comparing signed and unsigned
values. Using nonliteral formats can lead to vulnerabilities (such as the
famous attacks on FTP servers <http://seclists.org/bugtraq/1999/Sep/328>)

~~~
zokier
-Wall includes -Wsign-compare

~~~
1amzave
You must be a C++ user -- I (a C guy) immediately thought "what? No, that's in
-Wextra, not -Wall", checked the man page to confirm, and sure enough, it
differs (-Wall includes sign-compare when compiling C++, but not C).

------
afhof
Sometimes when compiling and trying to use system calls gcc will complain
about the functions not existing. This is usually because of some preprocessor
guard not being defined. An easy way to see what macros have been defined is
to run gcc with the "-E -dM" flags.

------
freedrull
-fmudflap always interested me, but I never seem to remember it when I'm actually confronting a hard to debug pointer issue.

------
pwpwp
My favorite is the "weak" attribute.
<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>

Makes separate compilation for languages that compile to C _much_ easier.

~~~
DarkShikari
My favorite _would_ be "pure" -- it's perfect for saying "this function
pointer, which calls an assembly function, doesn't muck with any of your data
structures, so you don't have to reload every single pointer after it
returns".

But unfortunately, since it was basically written only for libc purposes, it
doesn't work on function pointers, so it's useless for this purpose.

------
16s
Is it me, or is this statement from the article wrong:

 _"All warnings can be enabled with -w."_

man g++ says:

 _"-w Inhibit all warning messages."_

Inhibit means to prevent (not to enable).

~~~
billswift
It is case sensitive; -W enables more warnings, not -w, which as you noticed
suppresses them.

------
rnicholson
No love for --coverage?

~~~
freedrull
A similar article about C code coverage tools would be great.

