Windows 和 Linux 上的 C++ 编译:ifdef 开关
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6649936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ compiling on Windows and Linux: ifdef switch
提问by Sardathrion - against SE abuse
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 #ifdefthat once can use?
我想在 Linux 和 Windows 上运行一些 C++ 代码。有一些代码我只想包含在一个操作系统中,而不是另一个。是否有一次可以使用的标准#ifdef?
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.
这个问题确实是重复的,但这里的答案要好得多,尤其是被接受的答案。
采纳答案by Muhammad Anjum Kaiser
use:
用:
#ifdef __linux__
//linux code goes here
#elif _WIN32
// windows code goes here
#else
#endif
回答by Vitor
No, these defines are compiler dependent. What you can do, use your own set of defines, and set them on the Makefile. See this threadfor more info.
不,这些定义依赖于编译器。您可以做什么,使用您自己的一组定义,并将它们设置在 Makefile 上。有关更多信息,请参阅此线程。
回答by Igor Oks
It depends on the used compiler.
这取决于使用的编译器。
For example, Windows' definition can be WIN32
or _WIN32
.
例如,Windows 的定义可以是WIN32
或_WIN32
。
And Linux' definition can be UNIX
or __unix__
or LINUX
or __linux__
.
而 Linux 的定义可以是UNIX
or__unix__
或LINUX
or __linux__
。
回答by user703016
It depends on the compiler. If you compile with, say, G++ on Linux and VC++ on Windows, this will do :
这取决于编译器。如果您使用 Linux 上的 G++ 和 Windows 上的 VC++ 进行编译,则会执行以下操作:
#ifdef linux
...
#elif _WIN32
...
#else
...
#endif
回答by Ajay
This response isn't about macro war, but producing error if no matching platform is found.
此响应与宏观战争无关,而是在找不到匹配平台时产生错误。
#ifdef LINUX_KEY_WORD
... // linux code goes here.
#elif WINDOWS_KEY_WORD
... // windows code goes here.
#else
#error Platform not supported
#endif
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 timeand not at runtime
如果#error
不支持,您可以使用 static_assert (C++0x) 关键字。或者你可以实现自定义的 STATIC_ASSERT,或者只是声明一个大小为 0 的数组,或者有重复案例的开关。简而言之,在编译时而不是在运行时产生错误
回答by user1527227
You can do
你可以做
#if MACRO0
//code...
#elif MACRO1
//code...
#endif
Where Macro can be:
宏可以在哪里:
__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
回答by Yash
I know it is not answer but added if someone looking same in Qt
我知道这不是答案,但如果有人在 Qt 中看起来相同,则会添加
In Qt
在 Qt
https://wiki.qt.io/Get-OS-name-in-Qt
https://wiki.qt.io/Get-OS-name-in-Qt
QString Get::osName()
{
#if defined(Q_OS_ANDROID)
return QLatin1String("android");
#elif defined(Q_OS_BLACKBERRY)
return QLatin1String("blackberry");
#elif defined(Q_OS_IOS)
return QLatin1String("ios");
#elif defined(Q_OS_MAC)
return QLatin1String("osx");
#elif defined(Q_OS_WINCE)
return QLatin1String("wince");
#elif defined(Q_OS_WIN)
return QLatin1String("windows");
#elif defined(Q_OS_LINUX)
return QLatin1String("linux");
#elif defined(Q_OS_UNIX)
return QLatin1String("unix");
#else
return QLatin1String("unknown");
#endif
}