我想在Linux和Windows上运行一些c++代码。有些代码我只想包含在一个操作系统上,而不想包含在另一个操作系统上。有没有一次可以使用的标准#ifdef?
类似于:
#ifdef LINUX_KEY_WORD
... // linux code goes here.
#elif WINDOWS_KEY_WORD
... // windows code goes here.
#else
#error "OS not supported!"
#endif
这个问题确实是重复的,但这里的答案要好得多,特别是公认的答案。
发布于 2011-07-11 20:21:15
使用:
#ifdef __linux__
//linux code goes here
#elif _WIN32
// windows code goes here
#else
#endif
发布于 2014-03-25 06:54:02
您可以执行以下操作:
#if MACRO0
//code...
#elif MACRO1
//code...
#endif
…其中标识符可以是:
__linux__ Defined on Linux
__sun Defined on Solaris
__FreeBSD__ Defined on FreeBSD
__NetBSD__ Defined on NetBSD
__OpenBSD__ Defined on OpenBSD
__APPLE__ Defined on Mac OS X
__hpux Defined on HP-UX
__osf__ Defined on Tru64 UNIX (formerly DEC OSF1)
__sgi Defined on Irix
_AIX Defined on AIX
_WIN32 Defined on Windows
发布于 2011-07-11 20:21:49
这取决于所使用的编译器。
例如,Windows的定义可以是WIN32
或_WIN32
。
Linux的定义可以是UNIX
、__unix__
、LINUX
或__linux__
。
https://stackoverflow.com/questions/6649936
复制相似问题