设为首页 收藏本站
查看: 978|回复: 0

[经验分享] Mongodb Compile C++ Driver

[复制链接]

尚未签到

发表于 2015-7-6 07:29:33 | 显示全部楼层 |阅读模式
  之前发现直接编译mongo源码中的驱动,静态库的驱动会很大,在链接使用的时候会报很多链接错误。
  转而直接编译单独提供驱动源码,同样vc2008的版本也要做我的另一篇博文中修改,在这不多说,具体参见:
  http://www.iyunv.com/tangdoudou/p/3364015.html
  
  1.首先说明一点,这边是编译的vc2008的版本,因为Mongo现在提供代码只有vs2010的工程,代码里面有C11的东东,故直接移植到vs2008上是不行的,如果是编译2010请跳过。
  在做命令行执行任何命令之前,请执行vcvars32.bat,这个文件在你的vs2008的sdk目录下面。切忌!
  2.下载2.4.6的Mongodb C++ Driver
  3.按照http://www.iyunv.com/tangdoudou/p/3364015.html中对驱动做适应性的修改,主要修改为C11和WIN32的问题。
  4.编译boost(1.4.90),我编译的release版本,debug版本在链接的时候会报错:



Building Yourself
Download the boost source from boost.org. Move it to C:\boost\.
From the Visual Studio 2008 IDE, choose Tools.Visual Studio Command Prompt to get a command prompt with all PATH variables set nicely for the C++ compiler.
From the MongoDB source project, run buildscripts\buildboost.bat. Or, buildboost64.bat for the 64 bit version.
When using bjam, MongoDB expects
variant=debug for debug builds, and variant=release for release builds
threading=multi
link=static runtime-link=static for release builds
address-model=64 for 64 bit
  编译报错:



libcmt.lib(invarg.obj) : error LNK2005: __initp_misc_invarg 已经在 LIBCMTD.lib(invarg.obj) 中定义
1>libcmt.lib(invarg.obj) : error LNK2005: __invoke_watson 已经在 LIBCMTD.lib(invarg.obj) 中定义
1>libcmt.lib(invarg.obj) : error LNK2005: __set_invalid_parameter_handler 已经在 LIBCMTD.lib(invarg.obj) 中定义
1>libcmt.lib(invarg.obj) : error LNK2005: __get_invalid_parameter_handler 已经在 LIBCMTD.lib(invarg.obj) 中定义
1>libcmt.lib(invarg.obj) : error LNK2005: "void __cdecl _invoke_watson(unsigned short const *,unsigned short const *,unsigned short const *,unsigned int,unsigned int)" (?_invoke_watson@@YAXPBG00II@Z) 已经在 LIBCMTD.lib(invarg.obj) 中定义
1>libcmt.lib(invarg.obj) : error LNK2005: __invalid_parameter 已经在 LIBCMTD.lib(invarg.obj) 中定义
1>libcmt.lib(invarg.obj) : error LNK2005: "void __cdecl _invalid_parameter(unsigned short const *,unsigned short const *,unsigned short const *,unsigned int,unsigned int)" (?_invalid_parameter@@YAXPBG00II@Z) 已经在 LIBCMTD.lib(invarg.obj) 中定义
1>libcmt.lib(invarg.obj) : error LNK2005: ___pInvalidArgHandler 已经在 LIBCMTD.lib(invarg.obj) 中定义
1>libcpmtd.lib(xdebug.obj) : warning LNK4098: 默认库“libcmt.lib”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
  这个运行CLR差别导致的错误,我暂时也没办法解决,后面如果搞定了,在补上来。有答案的兄弟,看见了也请知会一声,谢谢。
  在CMD或者PowerShell中执行:



1 ./bjam stage variant=release  --with-filesystem --with-thread --with-date_time --with-program_options --layout=versioned threading=multi toolset=msvc-9.0 --build-type=complete link=static runtime-link=static
  如果不出意外的话,编译后boost/stage/lib文件夹下生产5个dll和15个lib文件。
  3.打开驱动根目录下面的SConstruct文件,在行尾添加:



1 env.Append(CPPPATH=["D:/mongodb/boost_1_49_0"], LIBPATH=["D:/mongodb/boost_1_49_0/stage/lib"])
  具体路径请替换为你的路径
  4.编译Mongodb Compile C++ Driver
  直接敲:scons
  不出意外的还是会有编译错误:(下面摘自:http://blog.iyunv.com/kuaile123/article/details/9963925)
DSC0000.jpg
提示text.h(89):#error temp error
这是因为scons没带使用 Unicode 字符集的参数,于是就默认使用多字节字符集
我们打开\mongo-cxx-driver-v2.2\src\mongo\util下的text.h文件



1 /* like toWideString but UNICODE macro sensitive */
2 # if !defined(_UNICODE)
3 #error temp error
4     inline std::string toNativeString(const char *s) { return s; }
5 # else
6     inline std::wstring toNativeString(const char *s) { return toWideString(s); }
7 # endif
将#error temp error加双斜线注释掉 //#error temp error,其实这么改不是很好,看后面你就会知道。
  可是输入后出错:
DSC0001.jpg
  这些无法解析的外部符号包含在WS2_32.lib,Dbghelp.lib中,在SConstruct文件中加入



1 env.Append(LIBS=['WS2_32','Dbghelp'])
  注:我直接SConstruct加上面这一行代码,并没有解决问题,反而在我的客户端程序中增加:



1 #include "mongo/client/dbclient.h"
2 #pragma  comment(lib, "mongoclient.lib")
3 #pragma comment(lib, "wsock32.lib")
4 #pragma comment(lib, "Dbghelp.lib")
  倒是解决这个问题了
  5.如果你也遇到“\mongo-cxx-driver-v2.4\src\mongo\util”中file.cpp文件中90行



1          _handle = CreateFileW(toNativeString(filename).c_str(),  
  报的字符编码问题,请将CreateFileW函数修改为多字节编码的函数:CreateFile,当然最好是加宏隔断控制下:



1         #ifdef _UNICODE //edit by tangpengchuan
2          _handle = CreateFileW(toNativeString(filename).c_str(),                 // filename
3                               (readOnly ? 0 : GENERIC_WRITE) | GENERIC_READ,    // desired access
4                               FILE_SHARE_WRITE | FILE_SHARE_READ,               // share mode
5                               NULL,                                             // security
6                               OPEN_ALWAYS,                                      // create or open
7                               FILE_ATTRIBUTE_NORMAL,                            // file attributes
8                               NULL);                                            // template
9         else
10          _handle = CreateFile(toNativeString(filename).c_str(),                 // filename
11                               (readOnly ? 0 : GENERIC_WRITE) | GENERIC_READ,    // desired access
12                               FILE_SHARE_WRITE | FILE_SHARE_READ,               // share mode
13                               NULL,                                             // security
14                               OPEN_ALWAYS,                                      // create or open
15                               FILE_ATTRIBUTE_NORMAL,                            // file attributes
16                               NULL);                                            // template
17         #endif
18         // end by tangpengchuan
  我个人觉得这么改很弱智,后面再改了
  6.把编译好的release版本的boost和Mongodb Compile C++ Driver链接到你的Client工程里面,记得要把运行时库改为MT!
  附上测试驱动Demo:



1 #include "mongo/client/dbclient.h"
2 #pragma  comment(lib, "mongoclient.lib")
3 #pragma comment(lib, "wsock32.lib")
4 #pragma comment(lib, "Dbghelp.lib")
5 using namespace mongo;
6 int main()
7 {   
8     std::string err;
9     mongo::DBClientConnection conn;
10     if (!conn.connect(std::string("172.17.182.86:27017"),err))
11     {
12         std::cout

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-83533-1-1.html 上篇帖子: 分布式文件存储的数据库开源项目MongoDB 下篇帖子: 用WPF+MongoDB开发房产信息收集器(4)——房产信息采集器总体介绍附程序下载
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表