This question already has an answer here:
I want to run some c++ code on Linux and Windows. There are some pieces of code that I want to include only for one operating system and not the other. Is there a standard #ifdef that once can use?
Something like:
#ifdef LINUX_KEY_WORD
... // linux code goes here.
#elif WINDOWS_KEY_WORD
... // windows code goes here.
#else
#error "OS not supported!"
#endif
The question is indeed a duplicate but the answers here are much better, especially the accepted one.
use:
No, these defines are compiler dependent. What you can do, use your own set of defines, and set them on the Makefile. See this thread for more info.
This response isn't about macro war, but producing error if no matching platform is found.
If
#error
is not supported, you may use static_assert (C++0x) keyword. Or you may implement custom STATIC_ASSERT, or just declare an array of size 0, or have switch that has duplicate cases. In short, produce error at compile time and not at runtimeIt depends on the compiler. If you compile with, say, G++ on Linux and VC++ on Windows, this will do :
I know it is not answer but added if someone looking same in Qt
In Qt
https://wiki.qt.io/Get-OS-name-in-Qt
It depends on the used compiler.
For example, Windows' definition can be
WIN32
or_WIN32
.And Linux' definition can be
UNIX
or__unix__
orLINUX
or__linux__
.