Thread: Detecting compiler

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    Detecting compiler

    Hi, i have some compiler dependent #include statement in my code (GCC is one include and in MSVC is another) Then i wanted to know how can i detect a compiler using preprocessor definitions.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, gcc uses __GNUC__ to identify itself. There is one for MS too, but I can't remember what it's called.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Yes, gcc uses __GNUC__ to identify itself. There is one for MS too, but I can't remember what it's called.

    --
    Mats
    For Visual Studio it would be: _MSC_VER

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Scarvenger View Post
    Hi, i have some compiler dependent #include statement in my code (GCC is one include and in MSVC is another) Then i wanted to know how can i detect a compiler using preprocessor definitions.
    The list of compiler and architecture defines provided by Salem is gold. Make sure to look at the top of the page for links to other defines (there's a section for Compilers).

    How big is your project? Scattering OS or compiler-dependent #ifdefs in your code becomes unmaintainable when projects get bigger. It is better to control platform-specific compilation through the build system, not inside the source files.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree with what brewbuck says, but if the amount of differences is quite small, it can be quite practical to put both the GNU and MS implementations in the same file [more likely that both gets updated at the same time that way]. Particularly if the ONLY difference is that you need to include x.h instead of y.h in one place.

    Scattering compiler/OS dependencies all over the place is generally not a good idea, and should be "fixed" by implementing an arch-independent function in one place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    /agrees with brewbuck and mastp
    Create platform independent wrappers around your OS/Compiler dependencies, so you minimise the amount of porting work you need to do, and keep the rest of the code nice and clean.

    Some of the more horrid projects I've worked on have had dozens of different flags, and hundreds of different logical combinations, and absolutely no ground rules as to how to use them. They just appeared anywhere at any time for no reason.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Ok, thank you! And, this is just one include statement on a two sources project hehe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  3. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  5. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM