我想在我的项目中使用MongoDB C ++驱动程序。我已经下载了源代码并按照附带建议构建了此驱动程序。当我构建项目时,使用此驱动程序会收到大量的链接器错误:
Undefined symbols for architecture x86_64:
"mongo::causedBy(std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&)", referenced from:
mongo::DBException::addContext(std::__1::basic_string<char,
std::__1::char_traits<char>,
std::__1::allocator<char> > const&)
in main.cpp.o
"mongo::UserException::appendPrefix(std::__1::basic_stringstream<char,
std::__1::char_traits<char>,
std::__1::allocator<char> >&) const",
referenced from:
vtable for mongo::ConnectException in main.cpp.o
"std::string::copy(char*, unsigned long, unsigned long) const", referenced from:
mongo::(anonymous namespace)::SSLManager::password_cb(char*, int, int, void*)
in libmongoclient.a(ssl_manager.o)
"std::string::find(char const*, unsigned long, unsigned long) const",
referenced from:
........
似乎链接c ++标准库存在一些问题。没有mongoDB驱动程序相关的代码一切正常。这个问题可能是什么原因以及如何解决?我使用的是cmake,OS是OS X 10.9.2。
UPD 链接器命令也是:
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld"
-demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o steven
-search_paths_first -headerpad_max_install_names CMakeFiles/steven.dir/main.cpp.o
/usr/local/lib/libboost_thread-mt.a /usr/local/lib/libboost_filesystem-mt.a
/usr/local/lib/libboost_system-mt.a /usr/local/lib/libboost_program_options-mt.a
/usr/local/lib/libmongoclient.a /usr/local/opt/openssl/lib/libssl.a
/usr/local/opt/openssl/lib/libcrypto.a -lc++ -lSystem
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
答案 0 :(得分:4)
最后,我设法解决了这个问题。原因是在编译驱动程序时使用不同版本的stdlib,并且链接了驱动程序库的整个应用程序。我将驱动程序库scorn build命令更改为:
scons --prefix=/usr/local --ssl install-mongoclient
--64 --c++11 --libc++ --osx-version-min=10.7
而不是:
scons --prefix=/usr/local --ssl install-mongoclient --64
现在链接器不产生错误,如果mongod正在运行,简单的教程应用程序甚至连接mongo
对于那些使用mongo db c ++驱动程序的项目,另一个有用的信息可能是CMakeLists.txt:
project(proj_name)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
set (CMAKE_CXX_FLAGS " -Wall -std=c++0x -stdlib=libc++ -v")
add_executable(${PROJECT_NAME} ${SRC_LIST})
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost COMPONENTS thread filesystem system program_options REQUIRED)
target_link_libraries(proj_name ${Boost_LIBRARIES} /usr/local/lib/libmongoclient.a /usr/local/opt/openssl/lib/libssl.a /usr/local/opt/openssl/lib/libcrypto.a)