There are multiple compilers that exist for C and C++, some of which are used to compile program for microcontrollers. Is there a way to limit our program to only compile in specific compilers? I would guess that there must be a way to do this using pre-processor directives.
Asked
Active
Viewed 346 times
1
-
Are you interested in determining specific standard support, or the specific compiler manufacturer also? Here's an example for the latter: http://coliru.stacked-crooked.com/a/1d77407c352fe882 – πάντα ῥεῖ Dec 16 '18 at 11:05
-
Most compilers (or their preprocessors) define macros that are unique to them. For example, gnu compilers define `__GNUC__` if compiling as C, and `__GNUG__` if compiling as C++, and those macros expand to values related to the compiler version number. To find what macros are defined for particular compilers, and what they expand to, you will need to read the documentation for each compiler. – Peter Dec 16 '18 at 11:15
-
2VS has `_MSC_VER`, clang has `__clang__`, there is `__APPLE__` for macOS, `__x86_64__` and `__i386__` for 32bits and 64bits intel processors for GCC, `__INTEL_COMPILER` for icpc, `_WIN32` for Windows (I think)... – Matthieu Brucher Dec 16 '18 at 11:21
-
@Peter: No compiler can ensure the macros it defines are unique to them. Clang-902.0.39.2 defines `__GNUC__` to be 4. – Eric Postpischil Dec 16 '18 at 12:37
1 Answers
1
As Matthieu Brucher said in his comment, some compiler vendors offer macros which you can ifdef
to check if you're compiling under them. Having said that, there is one(albeit ugly) way to limit your C++ from compiling under a specific compiler.
Let's say that you want to make your program not compile under clang
, you can do something like:
#ifdef __clang__
#error
#endif
This way, if your code is compiled under clang - it simply won't compile due to the fact that the code under ifdef fails to compile if the __clang__
macro is defined.

Elvir Crncevic
- 525
- 3
- 17
-
4You can use `#error` instead of `x=""`. It would be a clean way to do this. – Remo.D Dec 16 '18 at 12:07
-
2Compilers are not required not to compile non-conforming code (C 2018 5.1.1.3 1 footnote 9) unless it contains a `#error` not skipped due to conditional inclusion (C 2018 4 4). – Eric Postpischil Dec 16 '18 at 12:40
-