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.
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.
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.
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.
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.
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.
/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.
Ok, thank you! And, this is just one include statement on a two sources project hehe.