#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define THE_NUM 3
#define WIN 1
#define LINUX 2
#define MAC 3
void sayHello(){
#if PLATFORM == WIN
printf("Hello Windows\n");
#elif PLATFORM == Linux
printf("Hello Linux\n");
#elif PLATFORM == MAC
printf("Hello Mac\n");
#else
printf("Unknown Platform");
#endif
}
int main(){
printf("The num is %d\n", THE_NUM);
sayHello();
return EXIT_SUCCESS;
}
输出如下:
The num is 3
Hello Linux
但是经过测试时不准确的,经过查看其它资料,验证如下
int main(){
printf("The num is %d\n", THE_NUM);
sayHello();
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
//define something for Windows (32-bit and 64-bit, this part is common)
#ifdef _WIN64
//define something for Windows (64-bit only)
printf("It's _WIN64 ");
#else
//define something for Windows (32-bit only)
printf("It's something for Windows-32 ");
#endif
#elif __APPLE__
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR
// iOS Simulator
printf("It's iOS Simulator ");
#elif TARGET_OS_IPHONE
// iOS device
printf("It's TARGET_OS_IPHONE ");
#elif TARGET_OS_MAC
// Other kinds of Mac OS
printf("It's TARGET_OS_MAC \n");
#else
#error "Unknown Apple platform"
#endif
#elif __linux__
// linux
printf("It's __linux__ ");
#elif __unix__ // all unices not caught above
// Unix
printf("It's __unix__ ");
#elif defined(_POSIX_VERSION)
// POSIX
printf("It's POSIX ");
#else
# error "Unknown compiler"
#endif
return EXIT_SUCCESS;
}
在MacOS中进行测试,输出如下:
The num is 3
Hello Linux
It's TARGET_OS_MAC
在CentOS7中进行测试,输出如下:
The num is 3
Hello Linux
It's __linux__