If there's some cross-platform C/C++ code that should be compiled on Mac OS X, iOS, Linux, Windows, how can I detect them reliably during preprocessor process?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
Kind of a corollary answer: the people on [this site] have taken the time to make tables of macros defined for every OS/compiler pair.
For example, you can see that
_WIN32
is NOT defined on Windows with Cygwin (POSIX), while it IS defined for compilation on Windows, Cygwin (non-POSIX), and MinGW with every available compiler (Clang, GNU, Intel, etc.).Anyway, I found the tables quite informative and thought I'd share here.
As Jake points out, TARGET_IPHONE_SIMULATOR is a subset of TARGET_OS_IPHONE.
Also, TARGET_OS_IPHONE is a subset of TARGET_OS_MAC.
So a better approach might be:
There are predefined macros that are used by most compilers, you can find the list here. GCC compiler predefined macros can be found here. Here is an example for gcc:
The defined macros depend on compiler that you are going to use.
The
_WIN64
#ifdef
can be nested into the_WIN32
#ifdef
because_WIN32
is defined when targeting Windows, not only the x86 version. This prevents code duplication if some includes are common to both.