19

How to detect at compile time if I'm using gcc or icc?

(I was quite puzzled to find out that icc defines __GNUC__ -- and even __GNUC_MINOR__ and __GNUC_PATCHLEVEL__ ! why?)

osgx
  • 90,338
  • 53
  • 357
  • 513
Znorg
  • 681
  • 1
  • 7
  • 19
  • 3
    Clang also defines these, they are for compatibility purposes: you can just replace the `gcc` command with `icc`, with the exact same options, without anything breaking (or that's the idea). – rubenvb Apr 20 '11 at 21:31

5 Answers5

19

We use

#ifdef __INTEL_COMPILER

to split icc off, assuming gcc as a default.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
9

I believe you could check for __INTEL_COMPILER according to this.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • This is more transparent https://www.boost.org/doc/libs/1_59_0/boost/config/compiler/intel.hpp – alfC Mar 11 '21 at 02:24
1

You can make the processor output the defined macros in the preprocessor output and look for a macro that suits you. You can generated the preprocessor output like this:

icc  -dM -E -o foo.P foo.c

Then look at foo.P (since it is a text file). In my case, I found icc defined an __ICC macro with the version of the compiler. It didn't define any __INTEL_COMPILER though.

Genís
  • 1,468
  • 2
  • 13
  • 24
1

The reason ICC defines __GNUC__ etc. is because of code like yours that is inspecting compiler-specific macros and expects to see them...

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 1
    You make it sound like it is a) his fault, b) not a good thing to do without giving any explanation. – Zulan Feb 04 '16 at 08:24
  • 1
    Another way to put this is that ICC implements the GNU dialect of C (and C++), and that's what the presence of `__GNUC__` indicates. It doesn't mean that the compiler is the GNU implementation of the GNU C language. – Peter Cordes Nov 08 '16 at 18:16
1

Traditionally, compilers have defined a symbol of their own as well as their version as preprocessor symbols so that the code could be adapted (generally to work around bugs or specificities).

CLang has introduced a mechanism I had not seen so far, under the form of the __has_feature query. It does not replace the "work around bugs" practices (which is why CLang still exposes specific symbols) but allows a more natural style for querying the compiler capacities. I don't know if other compilers plan on defining such a facility.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722