3

I have been working with qt creator and recently tried to change the compiler from gcc to clang. Since I don't get any info (or can't see it) on whether this worked (I'm struggling to understand the interface) I wanted to ask if there's a way for my c++ code to print out the compiler under which it's being compiled.

  • May be duplicated with:http://stackoverflow.com/questions/4724925/compiler-version-name-and-os-detection-in-c take a look on it – Behnam Safari Mar 02 '14 at 12:07

2 Answers2

3

Compilers set certain #defines to help out with things like this.

In your case,

#ifdef __GNUC__ //GCC
//do whatever GCC-specific stuff you need to do here
#endif

#ifdef __clang__ //clang
//do whatever clang-specific stuff you need to do here
#endif

This page on SourceForge shows a list of such compiler-specific #define values.

EDIT: as pointed out in the comments, clang sets __GNUC__, and possibly __GNUC_MINOR__ and __GNUC_PATCHLEVEL__. You might be better off using a double test to make sure clang isn't misleading you:

#if defined(__GNUC__) && !defined(__clang__)
//do whatever GCC-specific stuff you need to do here
#endif
cf-
  • 8,598
  • 9
  • 36
  • 58
  • 3
    Note that clang++ is "gcc compatible", and thus defined `__GNUC__`! – Mats Petersson Mar 02 '14 at 12:09
  • As noted in the linked page, many compilers define `__GNUC__` to say they are compatible with GCC. Maybe better to check for e.g. `__GNUC_MINOR__`. – Some programmer dude Mar 02 '14 at 12:10
  • you can use #elif which won't require `__GNUC_MINOR__` – Jekyll Mar 02 '14 at 12:15
  • Thanks, edited my answer. I don't use clang so I wasn't familiar with that pitfall. – cf- Mar 02 '14 at 12:15
  • @Jekyll I think that's slightly risky, though, because it relies on the clang test going first. If op later hand-writes another compiler check and forgets about the `__GCC__` pitfall, that could come back to bite them. – cf- Mar 02 '14 at 12:17
  • @computerfreaker `#elif defined(__GNUC__) && !defined(__clang__)` also because `__GNUC_MINOR__` I suspect is defined for clang as well. you can run `clang -dM -E -x c++ /dev/null` to verify that. – Jekyll Mar 02 '14 at 12:26
  • 1
    @Jekyll I don't use clang so I can't verify whether it defines `__GNUC_MINOR__` or `__GNUC_PATCHLEVEL__`, but I'll edit my answer once more to reflect the possibility. – cf- Mar 02 '14 at 12:30
1

Use the informational macros of boost.

#include  <boost/config.hpp> 

#ifdef BOOST_CLANG

printf("Successfully changed to clang\n");

#endif
erenon
  • 18,838
  • 2
  • 61
  • 93