跨平台 C 语言开发
- 通过
WIN32宏来判断是否为 Windows 平台。64位 Windows 也会定义WIN32宏,此外还定义WIN64宏,有些编译器可能是Windows、__WIN32__、WIN32、_WIN64 - 通过
_MSC_VER宏判断是否是VC编译器 - 通过
__GNUC__宏来判断是否为 gcc 编译器 - Windows 平台上的 gcc 编译器支持
__declspec(dllimport)和__declspec(dllexport) - Linux 平台上 gcc 通过
__attribute__ ((visibility("default")))来导出函数 - VC不支持
inline关键字,要用__inline。可以用如下代码定义:C
#if defined(_MSC_VER) && !defined(__cplusplus) && !defined(inline) #define inline __inline #endif - 数组索引用
size_t类型、指针运算用uintptr_t,intptr_t,ptrdiff_t、平台无关整型用int32_t,int64_t - 64位平台上的
sizeof(long double)是16字节(VC不支持),但是实际使用长度是12字节,4字节填充的是随机数据(绝对大坑) - 如果 C 代码可能被 C++ 项目使用,最好根据
__cplusplus宏做下判断 - MacOSX的平台宏是
__APPLE__和__MACH__(这个没确认) - 补充几个编译器识别的宏:
__BORLANDC__、__llvm__、__clang__、__WATCOMC__
Predefined macros in C/C++ that tell you what the target platform is.

Predefined macros in C/C++ that tell you what the compiler is.

Non-model-specific macros that describe the processor in terms of its features

Predefined macros in C/C++ that tell you what language features are available

Macros that are specific to Intel Architecture processor targets

Macros for other than Intel Architecture processor targets
