Related
UPDATE
Solved the issue. The was the structure of the g++ command. This one worked for me:
g++ tutorial.cpp -I/opt/local/include -I/usr/include/boost -L/opt/local/lib -lmongoclient -lboost_thread -lboost_system -lboost_regex -o tutorial
OS: Ubuntu 16.04 64-bit
Following this tutorial:
https://mongodb.github.io/mongo-cxx-driver/legacy-v1/tutorial/
I'm able to build the mongodb-client using SCons with no issue.
mongo folder of header files here:
/opt/local/include
libmongoclient.a file here:
/opt/local/lib
Code is as follows, same as the tutorial:
#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;
}
This is the command I'm using to compile:
g++ -I/opt/local/include -L/opt/local/lib -pthread -lmongoclient -lboost_thread -lboost_system -lboost_filesystem -lboost_program_options tutorial.cpp -o tutorial
I receive the following error
/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
I've searched around Google, and saw some people with the same issue, but they didn't appear to be linking files correctly. As far as I can tell I'm doing everything correctly.
Any help would be greatly appreciated.
EDIT
Output if I change the command to:
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
In your g++ command, try to have tutorial.cpp first before the other libs. Some linkers needs to see the objects that need symbols first (from left to right)
The command:
g++ -o "webserver" ./webserver.o -lwtdbopostgres -lwtdbo -lwthttp -lwt -lboost_signals
The horrendous, horrendous output:
./webserver.o: In function `Wt::Dbo::Impl::QueryBase<Wt::Dbo::ptr<pool::db::Settings> >::statements(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int) const':
webserver.cpp (.text._ZNK2Wt3Dbo4Impl9QueryBaseINS0_3ptrIN4pool2db8SettingsEEEE10statementsERKSsSA_SA_ii[Wt::Dbo::Impl::QueryBase<Wt::Dbo::ptr<pool::db::Settings> >::statements(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int) const]+0x1b3): undefined reference to `Wt::Dbo::Impl::completeQuerySelectSql(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int, std::vector<Wt::Dbo::FieldInfo, std::allocator<Wt::Dbo::FieldInfo> > const&, bool)'
webserver.cpp:(.text._ZNK2Wt3Dbo4Impl9QueryBaseINS0_3ptrIN4pool2db8SettingsEEEE10statementsERKSsSA_SA_ii[Wt::Dbo::Impl::QueryBase<Wt::Dbo::ptr<pool::db::Settings> >::statements(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int) const]+0x25c): undefined reference to `Wt::Dbo::Impl::createQueryCountSql(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int, bool)'
Many, many more lines...
Extracting the error messages a bit:
undefined reference to `Wt::Dbo::Impl::completeQuerySelectSql
undefined reference to `Wt::Dbo::Impl::createQueryCountSql
undefined reference to `Wt::Dbo::Impl::createWrappedQueryCountSql
...
Why are these not found? It is being linked with -lwtdbo and -lwtdbopostgres, which is what the project settings came with. This is after I downloaded, compiled, and installed the latest wt versions manually. Previously I had gotten them using apt-get and I had a different set of errors, though still undefined-reference-related.
It's likely that libwtdbopostgres.so and libwtdbo.so aren't in your library path. Check your $LD_LIBRARY_PATH to see if the path those libs are located appears.
Alternatively try using -L to specific the path to your two libraries
ie:
g++ -o "webserver" -L/home/libs ./webserver.o -lwtdbopostgres -lwtdbo... etc..
I am quite new to C++ and I was trying to learn regular expressions using the Boost library. I am trying the following simple code.
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
/*
*
*/
int main()
{
const regex expression( "How to re" );
string string1 = "How to re";
bool match=regex_match(string1,expression);
if (match){
cout<<"Yes"<<endl;
}
return 0;
}
The problem is that a huge error is flagged when I try to run the file from the terminal.
The error flagged is the following:
Undefined symbols:
"boost::re_detail::get_mem_block()", referenced from:
boost::re_detail::save_state_init::save_state_init(boost::re_detail::saved_state**, boost::re_detail::saved_state**)in ccqa7fIw.o
boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::extend_stack()in ccqa7fIw.o
"boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::maybe_assign(boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&)", referenced from:
boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_match()in ccqa7fIw.o
"boost::re_detail::get_default_error_string(boost::regex_constants::error_type)", referenced from:
boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) constin ccqa7fIw.o
boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) constin ccqa7fIw.o
"boost::re_detail::put_mem_block(void*)", referenced from:
boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)in ccqa7fIw.o
boost::re_detail::save_state_init::~save_state_init()in ccqa7fIw.o
"boost::re_detail::raise_runtime_error(std::runtime_error const&)", referenced from:
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)in ccqa7fIw.o
"boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::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)", referenced from:
boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::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::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::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::basic_string<char, std::char_traits<char>, std::allocator<char> > >)in ccqa7fIw.o
"boost::re_detail::cpp_regex_traits_implementation<char>::transform_primary(char const*, char const*) const", referenced from:
boost::cpp_regex_traits<char>::transform_primary(char const*, char const*) constin ccqa7fIw.o
"boost::re_detail::cpp_regex_traits_implementation<char>::transform(char const*, char const*) const", referenced from:
boost::cpp_regex_traits<char>::transform(char const*, char const*) constin ccqa7fIw.o
"boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)", referenced from:
boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)in ccqa7fIw.o
"boost::re_detail::verify_options(unsigned int, boost::regex_constants::_match_flags)", referenced from:
boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::find_imp()in ccqa7fIw.o
"boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::find()", referenced from:
bool boost::regex_search<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::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::basic_string<char, std::char_traits<char>, std::allocator<char> > >)in ccqa7fIw.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I really cannot understand what is going on here; I was also looking at examples of regex codes from other threads but couldn't find any solution. Can you please help me? Thanks!
You do not link with the Boost regex library, it's not header only but have a library you have to link with as well.
Try
$ c++ -I $PATH/boost_1_52_0 filename.cpp -o filename -L$PATH/to/libraries -lboost_regex
You have to set the path to the library as well, as you seem to use a custom built Boost.
I wrote some code in source file sql_parse.cc of MySql 5.5.7rc. There I have used vector, allocator, etc but the compiler is not being linked with Standard Template Library(STL). Can anybody suggest me what should I do?
Here is the error msg :
libsql.a(sql_parse.cc.o): In function `std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >::push_back(std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)':
sql_parse.cc:(.text._ZNSt6vectorISsSaISsEE9push_backERKSs[std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >::push_back(std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)]+0x74): undefined reference to
`std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >
>::_M_insert_aux(__gnu_cxx::__normal_iterator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > > >, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)'
collect2: ld returned 1 exit status
You should've given the command line. I suspect that you use gcc for linking in which case you should either use g++ or add -lstdc++ option.
I got my stl code to compile with the following
g++ -std=c++11 file.cpp -o prog
undefined reference to
std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >
::_M_insert_aux(__gnu_cxx::__normal_iterator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > > >, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)'
That symbol is a member function of std::vector<std::string> and is a function template, so should be instantiated in your program, not in the C++ standard library.
Are you compiling with the -fno-implicit-templates option? Or using an explicit instantiation declaration (i.e. extern template) in your code and not providing the definition? Those would prevent the compiler from instantiation the template.
If you're telling the compiler not to provide implicit instantiations then you will need to add an explicit instantiation for each undefined reference:
template void std::vector<std::string>::_M_insert_aux(std::vector<std::string>::iterator, std::string const&);
#ifndef GLOBAL_H
#define GLOBAL_H
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
#endif //GLOBAL_H
Is a good start
I'm a newbie about boost. I compiled boost libraries with success (under mac os x). Now, I tried to build the very first example mentioned at boost website (including boost/ as include directory and boost/stage/lib as library directory, with netbeans) and got the following error
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/boost_ex1
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/main.o.d
g++ -c -g -I../../boost_1_44_0 -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp
mkdir -p dist/Debug/GNU-MacOSX
g++ -o dist/Debug/GNU-MacOSX/boost_ex1 build/Debug/GNU-MacOSX/main.o -L../../boost_1_44_0/stage/lib
Undefined symbols:
"boost::re_detail::get_mem_block()", referenced from:
boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::extend_stack()in main.o
"boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::maybe_assign(boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > const&)", referenced from:
boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_match()in main.o
"boost::re_detail::get_default_error_string(boost::regex_constants::error_type)", referenced from:
boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) constin main.o
boost::re_detail::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) constin main.o
"boost::re_detail::put_mem_block(void*)", referenced from:
boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)in main.o
"boost::re_detail::raise_runtime_error(std::runtime_error const&)", referenced from:
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)in main.o
"boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()", referenced from:
bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::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)in main.o
"boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::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)", referenced from:
boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::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::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::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::basic_string<char, std::char_traits<char>, std::allocator<char> > >)in main.o
"boost::re_detail::cpp_regex_traits_implementation<char>::transform_primary(char const*, char const*) const", referenced from:
boost::cpp_regex_traits<char>::transform_primary(char const*, char const*) constin main.o
"boost::re_detail::cpp_regex_traits_implementation<char>::transform(char const*, char const*) const", referenced from:
boost::cpp_regex_traits<char>::transform(char const*, char const*) constin main.o
"boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)", referenced from:
boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-MacOSX/boost_ex1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)
am I missing something?
Thanks!
Most Boost libraries are header-only:
they consist entirely of header files
containing templates and inline
functions, and require no
separately-compiled library binaries
or special treatment when linking.
However, the Boost.Regex library has a library you have to link with if you are using its code. For more details on this see Getting Started guide.
So the problem in your case is that you are not linking with Boost.Regex library, called libboost_regex.so. In order to do that, modify your project settings (or make file) so that it invokes gcc with -lboost_regex option. This option should come right after -L../../boost_1_44_0/stage/lib option that you already have, like this
g++ -o dist/Debug/GNU-MacOSX/boost_ex1 build/Debug/GNU-MacOSX/main.o -L../../boost_1_44_0/stage/lib -lboost_regex
Good luck!