之前发现直接编译mongo源码中的驱动,静态库的驱动会很大,在链接使用的时候会报很多链接错误。

转而直接编译单独提供驱动源码,同样vc2008的版本也要做我的另一篇博文中修改,在这不多说,具体参见:

http://www.cnblogs.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.cnblogs.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 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 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= for bit

编译报错:

libcmt.lib(invarg.obj) : error LNK2005: __initp_misc_invarg 已经在 LIBCMTD.lib(invarg.obj) 中定义
>libcmt.lib(invarg.obj) : error LNK2005: __invoke_watson 已经在 LIBCMTD.lib(invarg.obj) 中定义
>libcmt.lib(invarg.obj) : error LNK2005: __set_invalid_parameter_handler 已经在 LIBCMTD.lib(invarg.obj) 中定义
>libcmt.lib(invarg.obj) : error LNK2005: __get_invalid_parameter_handler 已经在 LIBCMTD.lib(invarg.obj) 中定义
>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) 中定义
>libcmt.lib(invarg.obj) : error LNK2005: __invalid_parameter 已经在 LIBCMTD.lib(invarg.obj) 中定义
>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) 中定义
>libcmt.lib(invarg.obj) : error LNK2005: ___pInvalidArgHandler 已经在 LIBCMTD.lib(invarg.obj) 中定义
>libcpmtd.lib(xdebug.obj) : warning LNK4098: 默认库“libcmt.lib”与其他库的使用冲突;请使用 /NODEFAULTLIB:library

这个运行CLR差别导致的错误,我暂时也没办法解决,后面如果搞定了,在补上来。有答案的兄弟,看见了也请知会一声,谢谢。

在CMD或者PowerShell中执行:

 ./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文件,在行尾添加:

 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.csdn.net/kuaile123/article/details/9963925

提示text.h(89):#error temp error

这是因为scons没带使用 Unicode 字符集的参数,于是就默认使用多字节字符集

我们打开\mongo-cxx-driver-v2.2\src\mongo\util下的text.h文件

 /* like toWideString but UNICODE macro sensitive */
# if !defined(_UNICODE)
#error temp error
inline std::string toNativeString(const char *s) { return s; }
# else
inline std::wstring toNativeString(const char *s) { return toWideString(s); }
# endif

将#error temp error加双斜线注释掉 //#error temp error,其实这么改不是很好,看后面你就会知道。

可是输入后出错:

这些无法解析的外部符号包含在WS2_32.lib,Dbghelp.lib中,在SConstruct文件中加入

 env.Append(LIBS=['WS2_32','Dbghelp'])

注:我直接SConstruct加上面这一行代码,并没有解决问题,反而在我的客户端程序中增加:

 #include "mongo/client/dbclient.h"
#pragma comment(lib, "mongoclient.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "Dbghelp.lib")

倒是解决这个问题了

5.如果你也遇到“\mongo-cxx-driver-v2.4\src\mongo\util”中file.cpp文件中90行

          _handle = CreateFileW(toNativeString(filename).c_str(),  

报的字符编码问题,请将CreateFileW函数修改为多字节编码的函数:CreateFile,当然最好是加宏隔断控制下:

         #ifdef _UNICODE //edit by tangpengchuan
_handle = CreateFileW(toNativeString(filename).c_str(), // filename
(readOnly ? : GENERIC_WRITE) | GENERIC_READ, // desired access
FILE_SHARE_WRITE | FILE_SHARE_READ, // share mode
NULL, // security
OPEN_ALWAYS, // create or open
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL); // template
else
_handle = CreateFile(toNativeString(filename).c_str(), // filename
(readOnly ? : GENERIC_WRITE) | GENERIC_READ, // desired access
FILE_SHARE_WRITE | FILE_SHARE_READ, // share mode
NULL, // security
OPEN_ALWAYS, // create or open
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL); // template
#endif
// end by tangpengchuan

我个人觉得这么改很弱智,后面再改了

6.把编译好的release版本的boost和Mongodb Compile C++ Driver链接到你的Client工程里面,记得要把运行时库改为MT!

附上测试驱动Demo:

 #include "mongo/client/dbclient.h"
#pragma comment(lib, "mongoclient.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "Dbghelp.lib")
using namespace mongo;
int main()
{
std::string err;
mongo::DBClientConnection conn;
if (!conn.connect(std::string("172.17.182.86:27017"),err))
{
std::cout << "cannot connect db:"<< err << std::endl;
}
else
{
std::cout << "connect db:"<< err << std::endl;
}
std::system("pause");
return ;
}

测试通过

如果你还有什么别的问题,欢迎来信交流。


本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

Mongodb Compile C++ Driver的更多相关文章

  1. mongodb MongoDB C#/.NET driver version

    The first column lists the driver version(s). C#/.NET Driver Version MongoDB 2.6 MongoDB 3.0 MongoDB ...

  2. MongoDB Node.js driver

    Node.js连接MongoDB的简单实例 安装Node.js driver npm install mongodb -save 连接 var MongodbClient = require('mon ...

  3. MongoDB C# / .NET Driver

    MongoDB C# Driver是官方提供的.NET C#驱动. Getting Started with the C# Driver C# Driver Tutorial C# Driver LI ...

  4. MongoDB数据库CXX Driver编译

    最近项目需要,想要测试下MongoDB读写大量小图片的速度(单纯文件系统io效率比较低,想试试NoSQL能不能提速), 因为使用C++开发,所以使用MongoDB的CXX驱动,需要自己编译,下面记录整 ...

  5. 【转】MongoDB C# / .NET Driver 中IMongoQuery的内部实现Query的用法

    MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似 json的bjson格式,因此可以存储比较复杂的数据类型. ...

  6. MongoDb C/java driver

    1,在linux下安装客户端连接windows下 的MongoDBServer.

  7. 编译安装MongoDB C++ Driver (win8.1 vs2013)

    在C++中调用mongodb的库函数需要安装mongodb的c++driver,需要自己编译,(自己搞了一天半 =_=''' ) 官网Build MongoDB From Source 说To bui ...

  8. MongoDB ODBC Driver for Data Integration with Power BI

    This guide will walk you through connecting Microsoft Power BI to a MongoDB DataSet using our MongoD ...

  9. MongoDB 4.6.1 c++ driver 编译

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sheismylife/article/details/25512251 这个版本号已经和之前不一样了 ...

随机推荐

  1. jsp页面遍历List<Map<String,Object>>

    多表联查会有此类结果出现, 查阅发现基本解决思路是双重遍历,获取map,entry.value等方法. 最终发现可以使用c:forEach单次遍历,map中的key值大写,即可得到object. Co ...

  2. 选iphone5可以正常编译运行 , 但是5s和6和6s都会编译报错

    选iphone5可以正常编译运行   但是5s和6和6s都会编译报错 iphone6编译报错iphone5s编译报错 解决办法是,Build settings里面把Architectures里面的$( ...

  3. web IDE Eclipse Che安装

    安装:使用安装器安装 使用安装器安装Che 环境 依赖 下载地址 通用 任何操作系统,java8,Git,Maven 3.0.5+,Docker 1.7+ 3.12.52 - 117MB window ...

  4. TensorFlow 实战(四)—— tensor 的认识

    tensorflow,即是 tensor flows,在 computation graph 中 flows(流动)的不是别人,正是 tensor: 1. tensor 基本属性 tensor 的名字 ...

  5. Android studio 无法启动安卓模拟器

    1.通过Android SDK Manager在extra中,找到并选中了那个Intel x86 Emulator Accelerator (HAXM) 2.手动启动HAXM    去对应的sdk\e ...

  6. Mac 开发PhoneGap 应用,怎样加入插件 barcodescaner

    搞了两天,最终悟出来了 PhoneGap 与 Cordova 的关系了,有人说是WebKit 与 Chrome 的关系,但我个人理解,两个东西再干一件事情,另外 PhoneGap 被Apache 收购 ...

  7. Java中的集合Map、HashMap、Hashtable、Properties、SortedMap、TreeMap、WeakHashMap、IdentityHashMap、EnumMap(五)

    Map Map用于保存具有映射关系的数据,因此Map集合里保存着两组值,一组值用于保存Map里的key,另一组值用于保存Map里的value,key和value都可以是任何引用类型的数据.Map的ke ...

  8. C# keybd_event用法 模拟键盘输入

    最近有业务需求,需要模拟键盘输入,所以了解了一下C#中keybd_event函数的用法.该函数能够产生WM_KEYUP或WM_KEYDOWN消息,即可以触发键盘事件. 函数引用如下: [DllImpo ...

  9. OVS 派OFPT_PORT_STATUS 流程

    依据openflow合约[OFP1.0-38],当从物理端口ovs datapath 添加,改动或者删除的时候.都会先运行详细动作.然后通过ofp_port_status异步消息告知Controlle ...

  10. WPF窗口继承实现统一风格的自定义窗口

    如何实现一个窗口的风格(style),让所有的窗口都继承这样同样的风格,包括标题栏,放大.缩小和关闭按钮. 那么,我们可不可以就建立一个Base窗口,然后将这个窗口的风格给设计好之后,所有的窗口都继承 ...