如何使用預(yù)處理器指令檢查OS?我需要我的代碼根據(jù)編譯它的操作系統(tǒng)來做不同的事情。我在找這樣的東西:#ifdef OSisWindows// do Windows-specific stuff#else// do Unix-specific stuff#endif有辦法嗎?有更好的方法來做同樣的事情嗎?
3 回答

猛跑小豬
TA貢獻(xiàn)1858條經(jīng)驗 獲得超8個贊
窗
_WIN32
_WIN64
Unix(Linux,*BSD,MacOSX)
unix
__unix
__unix__
MacOSX
__APPLE__
__MACH__
linux
__linux__
linux
__linux
FreeBSD
__FreeBSD__

一只甜甜圈
TA貢獻(xiàn)1836條經(jīng)驗 獲得超5個贊
gcc -dM -E - <NUL:
gcc -dM -E - </dev/null
WIN32 _WIN32 __WIN32 __WIN32__ __MINGW32__ WINNT __WINNT __WINNT__ _X86_ i386 __i386
unix __unix__ __unix

慕哥6287543
TA貢獻(xiàn)1831條經(jīng)驗 獲得超10個贊
#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 stringchar *get_platform_name() { return (PLATFORM_NAME == NULL) ? "" : PLATFORM_NAME;}int main(int argc, char *argv[]) { puts(get_platform_name()); return 0;}
Debian 8 窗戶(MinGW) 窗口(Cygwin)
- 3 回答
- 0 關(guān)注
- 465 瀏覽
添加回答
舉報
0/150
提交
取消