在OSX下编译报找不到malloc.h,经过查询是头文件位置不兼容,于是搜索资料,看如何通过宏来兼容不同平台。
在OSX下可以通过 gcc -arch i386 -dM -E - < /dev/null | sort
查看gcc定义的预编译宏。
可以添加如下代码进行兼容
#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 __ANDROID__
// android
#elif __linux__
// linux
#elif __unix__ // all unices not caught above
// Unix
#elif defined(_POSIX_VERSION)
// POSIX
#else
# error "Unknown compiler"
#endif
譬如malloc.h是这样的:
#ifdef __APPLE__
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif