在跨平台宏定义中包含extern “C”的位置问题

再写导出宏的时候,首先参考了同事的代码,发现他对extern “C”的处理办法是这样的

1、首先在某个头文件定义导出宏

#if defined(WIN32) || defined(_WINDLL)

#   ifdef ANTARES_EXPORT

#       define ANTARES_API __declspec(dllexport)

#   else

#       define ANTARES_API __declspec(dllimport)

#   endif

#else

#   if __GNUC__ >= 4

#       define ANTARES_API      __attribute__ ((visibility("default")))

#   else

#       define ANTARES_API

#   endif

#endif

2、在使用 ANTARES_API 声明函数的头文件中再使用下面的宏进行包含

#ifdef __cplusplus
extern "C" {
#endif

ANTARES_API 。。。

ANTARES_API。。。

。。。。。。

#ifdef __cplusplus
}
#endif

这样在头文件中比较繁琐,我最后的做法是这样的(因为是静态库,所以没有export或者import)

#if defined(WIN32) || defined(_WINDLL)
#   ifdef __cplusplus
#       define LISENCE_API extern "C"
#   else
#       define LISENCE_API
#   endif
#else
#   if __GNUC__ >= 4
#       ifdef __cplusplus
#           define LISENCE_API extern "C" __attribute__ ((visibility("default")))
#       else
#           define LISENCE_API __attribute__ ((visibility("default")))
#       endif
#   else
#       ifdef __cplusplus
#           define LISENCE_API extern "C"
#       else
#           define LISENCE_API
#       endif
#   endif
#endif

这样在头文件中就不必再纠结extern “C”的包含了

posted @ 2013-02-26 20:17  礼拜天  阅读(1608)  评论(0编辑  收藏  举报