我需要我的代码根据它被编译的操作系统做不同的事情。我在寻找这样的东西:

#ifdef OSisWindows
// do Windows-specific stuff
#else
// do Unix-specific stuff
#endif

有办法做到这一点吗?有没有更好的方法来做同样的事情?


当前回答

总结一下,这里有一些有用的链接。

GCC通用预定义宏 预定义的操作系统 MSDN预定义宏 多链接的NaudeaSoftware页面 维基百科! SourceForge的“用于标准、编译器、操作系统和硬件架构的预定义编译器宏的概述。” FreeBSD的“差异化操作系统” 各种预定义宏 libportable

其他回答

基于nadeaussoftware和Lambda Fairy的答案。

#include <stdio.h>

/**
 * Determination a platform of an operation system
 * Fully supported supported only GNU GCC/G++, partially on Clang/LLVM
 */

#if defined(_WIN32)
    #define PLATFORM_NAME "windows" // Windows
#elif defined(_WIN64)
    #define PLATFORM_NAME "windows" // Windows
#elif defined(__CYGWIN__) && !defined(_WIN32)
    #define PLATFORM_NAME "windows" // Windows (Cygwin POSIX under Microsoft Window)
#elif defined(__ANDROID__)
    #define PLATFORM_NAME "android" // Android (implies Linux, so it must come first)
#elif defined(__linux__)
    #define PLATFORM_NAME "linux" // Debian, Ubuntu, Gentoo, Fedora, openSUSE, RedHat, Centos and other
#elif defined(__unix__) || !defined(__APPLE__) && defined(__MACH__)
    #include <sys/param.h>
    #if defined(BSD)
        #define PLATFORM_NAME "bsd" // FreeBSD, NetBSD, OpenBSD, DragonFly BSD
    #endif
#elif defined(__hpux)
    #define PLATFORM_NAME "hp-ux" // HP-UX
#elif defined(_AIX)
    #define PLATFORM_NAME "aix" // IBM AIX
#elif defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
    #include <TargetConditionals.h>
    #if TARGET_IPHONE_SIMULATOR == 1
        #define PLATFORM_NAME "ios" // Apple iOS
    #elif TARGET_OS_IPHONE == 1
        #define PLATFORM_NAME "ios" // Apple iOS
    #elif TARGET_OS_MAC == 1
        #define PLATFORM_NAME "osx" // Apple OSX
    #endif
#elif defined(__sun) && defined(__SVR4)
    #define PLATFORM_NAME "solaris" // Oracle Solaris, Open Indiana
#else
    #define PLATFORM_NAME NULL
#endif

// Return a name of platform, if determined, otherwise - an empty string
const char *get_platform_name() {
    return (PLATFORM_NAME == NULL) ? "" : PLATFORM_NAME;
}

int main(int argc, char *argv[]) {
    puts(get_platform_name());
    return 0;
}

测试与GCC和叮当上:

Debian 8 窗口(MinGW) 窗口(Cygwin)

很抱歉外部的参考,但我认为它适合你的问题:

C/ c++提示:如何使用编译器预定义的宏检测操作系统类型

我在这里没有找到俳句的定义。总的来说,Haiku-os的定义很简单__HAIKU__

微软C/ c++编译器(MSVC)预定义宏可以在这里找到

我想你正在寻找:

_WIN32 -当编译目标是32位ARM、64位ARM、x86或x64时定义为1。否则,未定义的 _WIN64 -当编译目标是64位ARM或x64时定义为1。否则,未定义。

gcc编译器预定义宏可以在这里找到

我想你正在寻找:

__GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__

为预定义的适当编译器执行谷歌。

你可以在编译时使用预处理器指令作为警告或错误检查,你根本不需要运行这个程序,只是简单地编译它。

#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
    #error Windows_OS
#elif defined(__linux__)
    #error Linux_OS
#elif defined(__APPLE__) && defined(__MACH__)
    #error Mach_OS
#elif defined(unix) || defined(__unix__) || defined(__unix)
    #error Unix_OS
#else
    #error Unknown_OS
#endif

#include <stdio.h>
int main(void)
{
    return 0;
}