views:

172

answers:

3

I was recently porting a project from GCC to clang(in which I fixed a number of C GNU-isms). This got me thinking: what C GNU-isms(extensions to the C language supported in GCC, which are not standardized) exist? Is there a comprehensive list anywhere?

+8  A: 

Here is a pretty comprehensive list straight from GCC's website. There seems to be quite a lot, so I wish you the best of luck sifting through it!

http://gcc.gnu.org/onlinedocs/gcc-4.2.0/gcc/C-Extensions.html

Thanks, Braden McDorman

beta
I see you resisted suggesting RTFM ;-)
Clifford
A: 

Although there are many extensions, and I defer to Beta's answer for that, it is unlikely that your project relies upon many of them. It is possible to disable extensions in a GNU build, so simply doing that will give you advance warning of any potential incompatibilities in your code base.

You may encounter other problems such as the fact that GCC supports most C99 features, whereas some popular compilers do not (Microsoft VC++ specifically). So you may want to disable C99 features too when you test the code base.

Clifford
Actually, on (non-embedded) systems other than Windows, most of the compilers do support most of the C99 features. It is a subject of current controversy on the comp.std.c news group, so you could look at groups.google.com for information too.
Jonathan Leffler
@Jonathan: That may be because on non-Windows systems such as OSX and Linux, the de facto standard compiler *is* GCC. I am in fact an embedded systems developer, and support for many C99 features (especially the free ones like the new headers) is not uncommon. In fact since GCC is also prevalent in embedded systems, it is very common.
Clifford
Even the non-GCC compilers support most of C99 on most of the server-class systems (carefully avoiding the embedded space, where the rules may be different and where I don't have a good insight into the current practicalities - and 25+ year old experience is of essentially no help these days).
Jonathan Leffler
Your point is well taken, and there is no disagreement from me. But developers should be aware that if they use C99, although many compilers will accept it, the one most commonly used on Windows (which in turn is the most widely used OS on the desktop) will not, and since this is is a question of porting from GCC to some unspecified compiler, the consideration may be germane.
Clifford
In recognition of the uncited claim, I have changed "many" to "some". But even one is significant if used by many. ;)
Clifford
A: 

One of the nicest GNUisms I found was explicit key declaration while filling structures.

 struct canmsg_t {
      short     flags;
      int       cob;
      canmsg_id_t   id;
      unsigned long timestamp;
      unsigned int  length;
      unsigned char data[CAN_MSG_LENGTH];
 };

 canmsg_t msg = 
 {
      ["flags"] = 0x00;
      ["cob"]   = 0;
      ["id"]    = 0x534;
      ["timestamp"] = 0;
      ["length"] = 1;
      ["data"] = { 0 };
 }

This does not allow to skip members or reorder them, just throws an error if you do so, but with 100+ element structures this becomes invaluable.

SF.
When compiling C code GCC now also supports C99 named initialisers, which *do* allow you to skip and reorder members: `struct canmsg_t msg = { .flags = 0x00; .length = 1; .id = 0x534; };`
caf
It would be nice to know what Mike was porting *to*, since that may not support C99 either (MSVC++ for example).
Clifford