c++ 中的__declspec关键字

        其实__declspec关键字是Microsoft c++中专用的关键字,它配合着一些属性可以对标准C++进行扩充。这些属性有:align、allocate、
deprecated、 dllexport、dllimport、 naked、noinline、noreturn、nothrow、novtable、selectany、thread、property和uuid。
我们这里主要讲述的是函数从动态库中导入与导出的问题,置于其他的作用请大家自查,我不过多简述:
       _declspec(dllimport)   是说这个函数是从别的DLL导入的,供自己用的
       _declspec(dllexport)   是说这个函数要从本DLL导出的,是供别人用的
如果下在C++动态函数C导出的定义,只要项目属性-->C++-->Preprocessor中输入TEST_EXPORTS就可以导出函数,如果没有定义是导入
#if (defined WIN32 || defined _WIN32) && defined TEST_EXPORTS
#	define TEST_API_EXPORTS __declspec(dllexport)
#elif defined __GNUC__ && __GNUC__ >= 4
#	define TEST_API_EXPORTS __attribute__ ((visibility ("default")))
#else
#	define TEST_API_EXPORTS
#endif


#ifndef TEST_EXTERN_C
#	ifdef __cplusplus
#		define TEST_EXTERN_C extern "C"
#	else
#		define TEST_EXTERN_C
#	endif
#endif

#if defined WIN32 || defined _WIN32
#	define TEST_CDECL		__cdecl
#	define TEST_STDCALL	__stdcall
#else
#	define TEST_CDECL
#	define TEST_STDCALL
#endif

#ifndef TEST_API
#  define TEST_API(rettype) TEST_EXTERN_C TEST_API_EXPORTS rettype TEST_CDECL
#endif

#ifndef TEST_IMPL
#  define TEST_IMPL TEST_EXTERN_C
#endif

参考例子:
TEST_API(bool) Test_DLL(const char* str); ///声明函数,头文件中
TEST_IMPL bool Test_DLL(const char* str){ return true} //实现函数,源文件中
发布了109 篇原创文章 · 获赞 29 · 访问量 45万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章