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} //实现函数,源文件中
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值