代码的多平台支持

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifdef _WIN32
//define something for Windows (32-bit and 64-bit, this part is common)
#ifdef _WIN64
//define something for Windows (64-bit only)
#else
//define something for Windows (32-bit only)
#endif
#elif __APPLE__
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR
// iOS Simulator
#elif TARGET_OS_IPHONE
// iOS device
#elif TARGET_OS_MAC
// Other kinds of Mac OS
#else
# error "Unknown Apple platform"
#endif
#elif __linux__
// linux
#elif __unix__ // all unices not caught above
// Unix
#elif defined(_POSIX_VERSION)
// POSIX
#else
# error "Unknown compiler"
#endif

python

判断系统信息的代码

1
2
3
import platform
print(platform.system())
print(platform.release())
1
2
3
4
5
6
7
8
9
10
11
12
from sys import platform as _platform

if _platform == "linux" or _platform == "linux2":
# linux
elif _platform == "darwin":
# MAC OS X
elif _platform == "win32":
# Windows
elif _platform == "win64":
# Windows 64-bit
else
# others

cmake

1
2
3
4
5
6
7
if(WIN32)
aux_source_directory(os/win SOURCES)
elseif(APPLE)
aux_source_directory(os/mac SOURCES)
else(UNIX)
aux_source_directory(os/linux SOURCES)
endif(WIN32)

qmake

1
2
3
4
5
6
7
8
9
macx {
# mac only
}
unix:!macx{
# linux only
}
win32{

}

Reference