Are*_*nim 112 c++ macros cross-platform c-preprocessor
我正在编写一个跨平台的代码,它应该在linux,windows,Mac OS上编译.在窗户上,我必须支持视觉工作室和mingw.
有一些特定于平台的代码,我应该放在#ifdef .. #endif
环境中.例如,这里我放置了win32特定代码:
#ifdef WIN32
#include <windows.h>
#endif
Run Code Online (Sandbox Code Playgroud)
但是我如何识别linux和mac OS?我应该使用什么定义名称(或等)?
kar*_*lip 130
对于Mac OS:
#ifdef __APPLE__
Run Code Online (Sandbox Code Playgroud)
对于Windows上的MingW:
#ifdef __MINGW32__
Run Code Online (Sandbox Code Playgroud)
对于Linux:
#ifdef __linux__
Run Code Online (Sandbox Code Playgroud)
对于其他Windows编译器,检查此线程和这个其他几个编译器和架构.
Joh*_*mew 59
请参阅:http://predef.sourceforge.net/index.php
该项目#defines
为许多操作系统,编译器,语言和平台标准以及标准库提供了相当全面的预定义列表.
rub*_*nvb 45
这是我使用的:
#ifdef _WIN32 // note the underscore: without it, it's not msdn official!
// Windows (x64 and x86)
#elif __unix__ // all unices, not all compilers
// Unix
#elif __linux__
// linux
#elif __APPLE__
// Mac OS, not sure if this is covered by __posix__ and/or __unix__ though...
#endif
Run Code Online (Sandbox Code Playgroud)
编辑:虽然以上可能适用于基础知识,但请记住通过查看Boost.Predef参考页面来验证要检查的宏.或者直接使用Boost.Predef.
rva*_*lue 20
如果您正在编写C++,我建议不要强烈使用Boost库.
最新版本(1.55)包括一个新的Predef库,它涵盖了您正在寻找的内容,以及许多其他平台和架构识别宏.
#include <boost/predef.h>
// ...
#if BOOST_OS_WINDOWS
#elif BOOST_OS_LINUX
#elif BOOST_OS_MACOS
#endif
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
104726 次 |
最近记录: |