1.跨平台动态库头文件定义
#if (defined _WIN32 || defined _WIN64) # ifdef MY_EXPORTS # define MY_EXTERN extern "C" __declspec(dllexport) # else # define MY_EXTERN extern "C" __declspec(dllimport) # endif # define MY_API #else # ifdef __linux__ # define MY_EXTERN extern "C" __attribute__((visibility("default"))) # else # define MY_EXTERN # endif # define MY_API #endif
使用,函数声明
MY_EXTERN int MY_API MY_Init();
函数定义
MY_EXTERN int MY_API MY_Init() { return 0; }
2.跨平台配置
#if (defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)) #pragma warning(disable: 4819) //unicode字符警告 //MSVC Version (vs98/vs2003/vs2005/vs2008/vs2010) #if (_MSC_VER >= 1600) //vs2010 #pragma warning(disable: 4996) #define MSVC_2010 #elif (_MSC_VER >= 1500) //vs2008 #pragma warning(disable: 4996) #define MSVC_2008 #elif (_MSC_VER >= 1400) //vs2005 #define MSVC_2005 #elif (_MSC_VER >= 1310) //vs2003 #define _WIN32_WINNT 0x0500 #define MSVC_2003 #else //vs98 #define _WIN32_WINNT 0x0500 #pragma warning(disable: 4786) #pragma warning(disable: 4710) #define MSVC_98 #endif #if defined(_WIN32_WCE) #define OS_WINCE 1 #endif #if defined(_WIN64) #define OS_WINDOWS64 1 #endif #pragma warning(disable:4100) #pragma warning(disable:4127) #ifndef CALLBACK #define CALLBACK __stdcall #endif #include <winsock2.h> #include <ws2tcpip.h> #include <windows.h> #include <stdio.h> #include <stdlib.h> #ifdef WINAPI_FAMILY_PARTITION //avoid warning #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE) #define OS_WINPHONE 1 #if (_WIN32_WINNT == _WIN32_WINNT_WIN8) #define OS_WINPHONE8 1 #endif #endif #endif #ifndef OS_WINPHONE #include <Mswsock.h> #endif #define OS_WINDOWS 1 #elif (defined(__linux__) || defined(__APPLE__) || defined(__SYMBIAN32__) || defined(ANDROID)) #include <unistd.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include <netdb.h> #include <errno.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <net/if.h> #if (defined(__SYMBIAN32__) || defined(ANDROID)) #include <sys/stat.h> #endif #define OS_POSIX 1 #define WAIT_TIMEOUT 258 #if defined(__LP64__) #define OS_POSIX64 1 #endif #if defined (__APPLE__) #include<TargetConditionals.h> #include <sys/stat.h> #include <sys/sockio.h> #define OS_APPLE 1 #if (defined(TARGET_OS_IPHONE) && (TARGET_OS_IPHONE == 1)) #define OS_IPHONE 1 #elif (defined(TARGET_OS_MAC) && (TARGET_OS_MAC == 1)) #define OS_MAC 1 #endif #endif #if defined(__SYMBIAN32__) #define OS_S60 1 #endif #if defined(ANDROID) #define OS_ANDROID 1 #endif #if ((defined(__arm__) || defined(__ARMEL__)) && defined(__linux__)) #define OS_ARMLINUX 1 #endif #ifndef CALLBACK #define CALLBACK #else #undef CALLBACK #define CALLBACK #endif #else #error OS Not Implement Yet. #endif // end #if (defined(WIN32) || defined(_WIN32_WCE))