我下载的版本是:mongo-cxx-driver-v2.2,这个版本使用了scons进行编译,在目录中输入scons命令即可编译。
注意:该版本如果使用boost1.5X将会出现filesystem的版本错误,建议使用1.49 1.48等。
先去src目录打开SConscript.client文件,把,env.SharedLibrary(‘mongoclient’, clientSource),前面的注释去掉,这样就可以编译动态库了。开始会出现这样的信息: scons: Reading SConscript files … Checking for C++ library boost_thread-mt… (cached) no Checking for C++ library boost_thread… yes Checking for C++ library boost_filesystem-mt… no Checking for C++ library boost_filesystem… yes Checking for C++ library boost_system-mt… no Checking for C++ library boost_system… yes 说明scons已经找到了所需要的三个boost库,注意到,mt版本的没有找到,没关系,mt是multi thread的缩写,多线程库,有了那三个其实就可以了。 在最后却爆出了一些boost的方法Undefine的错误,说明动态库连接时出了问题了。如果你不会python,不要惊慌,看一下最后的一条编译命令为何出错。
g++ -o build/libmongoclient.so -Wl,–as-needed -Wl,-zdefs -pthread -shared build/mongo/bson/oid.os build/mongo/buildinfo.os build/mongo/client/authentication_table.os build/mongo/client/clientAndShell.os build/mongo/client/clientOnly.os build/mongo/client/connection_factory.os build/mongo/client/connpool.os build/mongo/client/dbclient.os build/mongo/client/dbclient_rs.os build/mongo/client/dbclientcursor.os build/mongo/client/distlock.os build/mongo/client/gridfs.os build/mongo/client/model.os build/mongo/client/syncclusterconnection.os build/mongo/db/jsobj.os build/mongo/db/json.os build/mongo/db/lasterror.os build/mongo/db/namespace.os build/mongo/db/nonce.os build/mongo/db/dbmessage.os build/mongo/pch.os build/mongo/util/assert_util.os build/mongo/util/background.os build/mongo/util/base64.os build/mongo/util/concurrency/rwlockimpl.os build/mongo/util/concurrency/spin_lock.os build/mongo/util/concurrency/synchronization.os build/mongo/util/concurrency/task.os build/mongo/util/concurrency/thread_pool.os build/mongo/util/concurrency/mutexdebugger.os build/mongo/util/debug_util.os build/mongo/util/stacktrace.os build/mongo/util/file_allocator.os build/mongo/util/histogram.os build/mongo/util/intrusive_counter.os build/mongo/util/log.os build/mongo/util/md5.os build/mongo/util/md5main.os build/mongo/util/net/httpclient.os build/mongo/util/net/listen.os build/mongo/util/net/message.os build/mongo/util/net/message_port.os build/mongo/util/net/sock.os build/mongo/util/password.os build/mongo/util/ramlog.os build/mongo/util/signal_handlers.os build/mongo/util/stringutils.os build/mongo/util/text.os build/mongo/util/time_support.os build/mongo/util/timer.os build/mongo/util/trace.os build/mongo/util/util.os -lboost_thread -lboost_filesystem -lboost_system
其实你一看就知道了,连接动态库需要使用-L指定动态库的路径或者说你默认的动态库搜索路径中没有他需要的那三个boost库,所以去SConstruct,查看脚本。发现上面有个AddOption(“–extrapath”,……,其实这个就是指定编译路径的命令,你可以这样编译:scons –extrapath=/opt/local,这样脚本会搜索/opt/local下面的lib和lib64文件夹,具体可以参考脚本中的方法:
1 2 3 4 5 | def addExtraLibs(s): for x in s.split( "," ): if os.path.exists(x): env.Append(CPPPATH = [x + "/include" , x], LIBPATH = [x + "/lib" , x + "/lib64" ]) |
其中的参数s就是你加在选项后面的路径了,你也可以直接修改脚本使其编译通过:
#if GetOption( “extrapath” ) is not None:
# addExtraLibs( GetOption( “extrapath” ) ) addExtraLibs(“/usr/local”)把上面两行注释,加入第三行,比如你的boost库在/usr/local/lib下面,这样就可以了。重新编译动态库时候,你会看到命令中多了两个路径-L/usr/local/lib /usr/local/lib64