aso
aso

Reputation: 1316

Preprocessor directives and #error

I want to show error when someone try to compile my code under other system than WIN32 and LINUX. But this code:

#ifdef WIN32
    // Some code here for windows
#elif LINUX
    // Some code for linux
#else
#error OS unsupported!
#endif

But this gives me an error:

#error OS unsupported

and compiler doesn't say anything else, just error. What is wrong?

Upvotes: 0

Views: 634

Answers (2)

syam
syam

Reputation: 15069

Two issues here:

  • your #elif does not test for the mere existence of the symbol, but for its truth (ie. defined and non-zero). You should use #elif defined(...) and, to be consistent, #if defined(...) at the start.

  • the symbols you are matching for are wrong. You should use, respectively, _WIN32 and __linux__. See this reference for more platforms.

Upvotes: 2

Jonathan Wakely
Jonathan Wakely

Reputation: 171283

LINUX is not a standard predefined macro. You probably want to check for __linux not LINUX

I know some code checks for _WIN32 but I don't know what's correct on Windows.

Upvotes: 1

Related Questions