1

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.

quantum231
  • 2,420
  • 3
  • 31
  • 53
  • 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
  • 2
    VS 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 Answers1

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