DLL封装thrift客户端

本文档介绍了如何在DLL中封装thrift客户端。首先,准备thrift-0.9.2、boost库和libevent库。接着,编译libthrift和libthriftnb静态库,配置编译环境,添加包含目录和库目录。然后,使用thrift编译器生成头文件和源文件。最后,在DLL项目中添加生成的文件,配置相关路径和依赖项,实现thrift客户端的DLL封装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于更换了数据库,因此要在新写接口函数DLL,封装thrift的客户端。thrift官网资料不够详细,在网上查阅各种资料后,终于成功,在此总结一下。


1准备

下载 

thrift-0.9.2.tar.gz
Thrift compiler for Windows (thrift-0.9.2.exe) 

0.9.3版本下缺少文件,编译时报错,无法成功,在这上面花费了太多时间。

boost库1.52版本,编译时的命令

bjam stage --toolset=msvc-10.0  link=shared runtime-link=shared threading=multi debug release
bjam stage --toolset=msvc-10.0 link=static runtime-link=static threading=multi debug release
bjam stage --toolset=msvc-10.0 threading=multi debug release

需要哪种boost编译出来的静态文件,可根据后面工程报错重新编译,如果不确定的话

libevent库,libevent-2.0.21-stable


2 编译libthrift和libthriftnb静态文件

进入\thrift-0.9.2\lib\cpp,VS2010打开Thrift.sln,有libthrift,libthriftnb两个工程。

两个工程的区别是,libthriftnb工程是非阻塞(non-blocking)模式的服务器,非阻塞模式需要依赖libevent库。


libthrift工程配置:
libthrift>属性->C/C++->常规->附加包含目录->\boost\boost_1_52
libthrift>属性->库管理器->常规->附加库目录->\boost\boost_1_52\stage\lib

此外,还需要openssl,下载对应编译器的库文件,libthrift>属性->C/C++->常规->附加包含目录->openssl-1.0.1q-vs2010\include


libthriftnb工程配置:
libthriftnb>属性->C/C++->常规->附加包含目录->\boost\boost_1_52
\libevent-2.0.21-stable
\libevent-2.0.21-stable\include
\libevent-2.0.21-stable\WIN32-Code

libthriftnb>属性->库管理器->常规->附加库目录->
\boost\boost_1_52\stage\lib   


完成配置后,就会成功生成libthrift.lib文件和libthriftnb.lib文件。


3生成.h 和.cpp文件

在命令行窗口,用Thrift compiler for Windows (thrift-0.9.2.exe) 生成对应文件:thrift-0.9.2.exe  --gen  cpp  hello.thrift



4创建DLL工程

在DLL项目中,添加文件


配置工程:

属性->C/C++->常规->附加包含目录->\boost\boost_1_52
属性->C/C++->常规->附加包含目录->\thrift-0.9.2\lib\cpp\src
属性->C/C++->常规->附加包含目录->\thrift-0.9.2\lib\cpp\src\thrift
 
属性->连接器->附加库目录->\boost\boost_1_52\stage\lib
属性->连接器->附加库目录->\thrift-0.9.2\lib\cpp\Debug

附加依赖项 libthrift.lib

对应的client文件是自己写的,部分代码是:

boost::shared_ptr<TSocket> socket(new TSocket(strParameters[0], atoi(strParameters[1])));

		boost::shared_ptr<TTransport> transportTemp(new TBufferedTransport(socket));
		transportRTDB = transportTemp;

		boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transportRTDB));

		client = new ::com::gdtianren::thrift::RTDBClient(protocol);

		transportRTDB->open();


配置完成后,就可生成DLL,并在server调用对应方法。

参考资料



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值