C ++ MongoDB遗留客户端教程编译错误

时间:2016-11-21 01:30:26

标签: c++ linux mongodb

更新

解决了这个问题。这是g ++命令的结构。这个对我有用:

g++ tutorial.cpp -I/opt/local/include -I/usr/include/boost -L/opt/local/lib -lmongoclient -lboost_thread -lboost_system -lboost_regex -o tutorial

操作系统: Ubuntu 16.04 64位

遵循本教程: https://mongodb.github.io/mongo-cxx-driver/legacy-v1/tutorial/

我能够使用SCons构建mongodb-client而没有任何问题。

头文件的

mongo 文件夹:

/opt/local/include

libmongoclient.a 文件:

/opt/local/lib

代码如下,与教程相同:

#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h" // for the driver

void run() {
    mongo::DBClientConnection c;
    c.connect("localhost");
}

int main() {
    mongo::client::initialize();
    try {
        run();
        std::cout << "connected ok" << std::endl;
    } catch( const mongo::DBException &e ) {
        std::cout << "caught " << e.what() << std::endl;
    }
    return EXIT_SUCCESS;
}

这是我用来编译的命令:

g++ -I/opt/local/include -L/opt/local/lib -pthread -lmongoclient -lboost_thread -lboost_system -lboost_filesystem -lboost_program_options tutorial.cpp -o tutorial

我收到以下错误

/tmp/cceJe78D.o: In function `run()':
tutorial.cpp:(.text+0x34): undefined reference to `mongo::DBClientConnection::DBClientConnection(bool, mongo::DBClientReplicaSet*, double)'
/tmp/cceJe78D.o: In function `main':
tutorial.cpp:(.text+0x12f): undefined reference to `mongo::client::Options::Options()'
tutorial.cpp:(.text+0x148): undefined reference to `mongo::client::initialize(mongo::client::Options const&)'
/tmp/cceJe78D.o: In function `__static_initialization_and_destruction_0(int, int)':
tutorial.cpp:(.text+0x299): undefined reference to `boost::system::generic_category()'
tutorial.cpp:(.text+0x2a5): undefined reference to `boost::system::generic_category()'
tutorial.cpp:(.text+0x2b1): undefined reference to `boost::system::system_category()'
/tmp/cceJe78D.o: In function `mongo::DBException::DBException(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
tutorial.cpp:(.text._ZN5mongo11DBExceptionC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi[_ZN5mongo11DBExceptionC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi]+0x21): undefined reference to `vtable for mongo::DBException'
/tmp/cceJe78D.o: In function `mongo::DBException::~DBException()':
tutorial.cpp:(.text._ZN5mongo11DBExceptionD2Ev[_ZN5mongo11DBExceptionD5Ev]+0x10): undefined reference to `vtable for mongo::DBException'
/tmp/cceJe78D.o: In function `mongo::DBException::addContext(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tutorial.cpp:(.text._ZN5mongo11DBException10addContextERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN5mongo11DBException10addContextERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x33): undefined reference to `mongo::causedBy(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cceJe78D.o: In function `mongo::UserException::UserException(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tutorial.cpp:(.text._ZN5mongo13UserExceptionC2EiRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN5mongo13UserExceptionC5EiRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x2a): undefined reference to `vtable for mongo::UserException'
/tmp/cceJe78D.o: In function `mongo::UserException::~UserException()':
tutorial.cpp:(.text._ZN5mongo13UserExceptionD2Ev[_ZN5mongo13UserExceptionD5Ev]+0xd): undefined reference to `vtable for mongo::UserException'
/tmp/cceJe78D.o: In function `mongo::DBClientConnection::~DBClientConnection()':
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0xe): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x1a): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x2c): undefined reference to `mongo::DBClientConnection::_numConnections'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0xbb): undefined reference to `mongo::DBClientBase::~DBClientBase()'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x189): undefined reference to `mongo::DBClientBase::~DBClientBase()'
/tmp/cceJe78D.o: In function `mongo::DBClientConnection::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tutorial.cpp:(.text._ZN5mongo18DBClientConnection7connectERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN5mongo18DBClientConnection7connectERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x78): undefined reference to `mongo::HostAndPort::HostAndPort(mongo::StringData const&)'
/tmp/cceJe78D.o:(.gcc_except_table+0x50): undefined reference to `typeinfo for mongo::DBException'
/tmp/cceJe78D.o:(.rodata._ZTVN5mongo16ConnectExceptionE[_ZTVN5mongo16ConnectExceptionE]+0x30): undefined reference to `mongo::UserException::appendPrefix(std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&) const'
/tmp/cceJe78D.o:(.rodata._ZTVN5mongo16ConnectExceptionE[_ZTVN5mongo16ConnectExceptionE]+0x40): undefined reference to `mongo::DBException::toString[abi:cxx11]() const'
/tmp/cceJe78D.o:(.rodata._ZTVN5mongo18AssertionExceptionE[_ZTVN5mongo18AssertionExceptionE]+0x40): undefined reference to `mongo::DBException::toString[abi:cxx11]() const'
/tmp/cceJe78D.o:(.rodata._ZTIN5mongo16ConnectExceptionE[_ZTIN5mongo16ConnectExceptionE]+0x10): undefined reference to `typeinfo for mongo::UserException'
/tmp/cceJe78D.o:(.rodata._ZTIN5mongo18AssertionExceptionE[_ZTIN5mongo18AssertionExceptionE]+0x10): undefined reference to `typeinfo for mongo::DBException'
collect2: error: ld returned 1 exit status

我在Google周围搜索过,看到有些人遇到同样的问题,但他们似乎没有正确链接文件。据我所知,我正在做的一切正确。

非常感谢任何帮助。

修改

如果我将命令更改为:

,则输出

g++ tutorial.cpp -I/opt/local/include -I/usr/include/boost -L/opt/local/lib -pthread -lmongoclient -lboost_thread -lboost_system -lboost_filesystem -lboost_program_options -o tutorial

/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
/usr/include/boost/regex/v4/basic_regex.hpp:380: undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:1135: undefined reference to `boost::re_detail::put_mem_block(void*)'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::cpp_regex_traits<char>::transform_primary(char const*, char const*) const':
/usr/include/boost/regex/v4/cpp_regex_traits.hpp:965: undefined reference to `boost::re_detail::cpp_regex_traits_implementation<char>::transform_primary(char const*, char const*) const'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::cpp_regex_traits<char>::transform(char const*, char const*) const':
/usr/include/boost/regex/v4/cpp_regex_traits.hpp:961: undefined reference to `boost::re_detail::cpp_regex_traits_implementation<char>::transform(char const*, char const*) const'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `void boost::re_detail::raise_error<boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > >(boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::error_type)':
/usr/include/boost/regex/pattern_except.hpp:75: undefined reference to `boost::re_detail::raise_runtime_error(std::runtime_error const&)'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) const':
/usr/include/boost/regex/v4/cpp_regex_traits.hpp:448: undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::extend_stack()':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:213: undefined reference to `boost::re_detail::get_mem_block()'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::save_state_init::save_state_init(boost::re_detail::saved_state**, boost::re_detail::saved_state**)':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:107: undefined reference to `boost::re_detail::get_mem_block()'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_imp()':
/usr/include/boost/regex/v4/perl_matcher_common.hpp:208: undefined reference to `boost::re_detail::verify_options(unsigned int, boost::regex_constants::_match_flags)'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::save_state_init::~save_state_init()':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:115: undefined reference to `boost::re_detail::put_mem_block(void*)'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_prefix()':
/usr/include/boost/regex/v4/perl_matcher_common.hpp:333: undefined reference to `boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::maybe_assign(boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&)'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::save_state_init::~save_state_init()':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:115: undefined reference to `boost::re_detail::put_mem_block(void*)'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)':
/usr/include/boost/regex/v4/perl_matcher.hpp:365: undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
/opt/local/lib/libmongoclient.a(dbclient.o): In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_match()':
/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:991: undefined reference to `boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::maybe_assign(boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&)'
collect2: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:1)

在你的g ++命令中,首先尝试在其他库之前使用 tutorial.cpp 。一些链接器需要首先看到需要符号的对象(从左到右)