I'm having trouble with passing a constant class through a function.
// test the constructors
auto CName nameOne("Robert", "Bresson");
const CName nameTwo = nameOne;
auto CName nameThree;
// display the contents of each newly-constructed object...
// should see "Robert Bresson"
cout << "nameOne = ";
nameOne.WriteFullName();
cout << endl;
// should see "Robert Bresson" again
cout << "nameTwo = ";
nameTwo.WriteFullName();
cout << endl;
As soon as the compiler hits nameTwo.WriteFullName() I get the error of abandoning qualifiers. I know that the class is a constant however I can't figure out how to work around it.
The function is in a header file written as so:
void const WriteFullName(ostream& outstream = cout)
{
outstream << m_first << ' ' << m_last;
}
I receive this error when const is put in back of the function header
main.cpp:(.text+0x51): undefined reference to `CName::CName()'
main.cpp:(.text+0x7c): undefined reference to `CName::WriteFullName(std::basic_ostream<char, std::char_traits<char> >&) const'
main.cpp:(.text+0xbb): undefined reference to `CName::WriteFullName(std::basic_ostream<char, std::char_traits<char> >&) const'
main.cpp:(.text+0xf7): undefined reference to `CName::WriteFullName(std::basic_ostream<char, std::char_traits<char> >&) const'
main.cpp:(.text+0x133): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, CName&)'
main.cpp:(.text+0x157): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x1f4): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x22b): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x25f): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x320): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)'
main.cpp:(.text+0x347): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, CName&)'
What you have is linker errors, which means that you don't link with all source files. I'm guessing that the code for the CName class is in a separate source file, and you forgot to build/link with it.
Edit: Compiling and linking multiple files
To make a program from more than one source file, you have to pass all the source files on the command line, like this:
$ g++ main.cpp cname.cpp -o myprogram
The above command uses both source files, and the -o option tells the compiler what to name the program.
You can also compile each source file separately and then link them together:
$ g++ main.cpp -c -o main.o
$ g++ cname.cpp -c -o cname.o
$ g++ main.o cname.o -o myprogram
The -c option tells the compiler to create an object file, that can later be used to link your program. The last command will not actually compile any sources, but instead uses the two object files to create the program.
Related
I'm using draco and boost together. Draco is working well after this question.
I have tried to install boost and i think its done.
Now i try to run this command in vs-code on wsl :
g++ -I ~/local/include -L ~/local/lib -o prog ObjContainer.cpp -ldracodec -lboost_system
errors:
ObjContainer.cpp:(.text._ZN5boost7archive6detail15common_iarchiveINS0_15binary_iarchiveEED2Ev[_ZN5boost7archive6detail15common_iarchiveINS0_15binary_iarchiveEED5Ev]+0x22): undefined reference to `boost::archive::detail::basic_iarchive::~basic_iarchive()'
/tmp/ccqY0aGT.o: In function `boost::archive::binary_iarchive_impl<boost::archive::binary_iarchive, char, std::char_traits<char> >::~binary_iarchive_impl()':
ObjContainer.cpp:(.text._ZN5boost7archive20binary_iarchive_implINS0_15binary_iarchiveEcSt11char_traitsIcEED2Ev[_ZN5boost7archive20binary_iarchive_implINS0_15binary_iarchiveEcSt11char_traitsIcEED5Ev]+0x32): undefined reference to `boost::archive::basic_binary_iprimitive<boost::archive::binary_iarchive, char, std::char_traits<char> >::~basic_binary_iprimitive()'
/tmp/ccqY0aGT.o: In function `boost::archive::binary_oarchive_impl<boost::archive::binary_oarchive, char, std::char_traits<char> >::binary_oarchive_impl(std::ostream&, unsigned int)':
ObjContainer.cpp:(.text._ZN5boost7archive20binary_oarchive_implINS0_15binary_oarchiveEcSt11char_traitsIcEEC2ERSoj[_ZN5boost7archive20binary_oarchive_implINS0_15binary_oarchiveEcSt11char_traitsIcEEC5ERSoj]+0x55): undefined reference to `boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive, char, std::char_traits<char> >::basic_binary_oprimitive(std::basic_streambuf<char, std::char_traits<char> >&, bool)'
ObjContainer.cpp:(.text._ZN5boost7archive20binary_oarchive_implINS0_15binary_oarchiveEcSt11char_traitsIcEEC2ERSoj[_ZN5boost7archive20binary_oarchive_implINS0_15binary_oarchiveEcSt11char_traitsIcEEC5ERSoj]+0xab): undefined reference to `boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive, char, std::char_traits<char> >::~basic_binary_oprimitive()'
/tmp/ccqY0aGT.o: In function `boost::archive::binary_iarchive_impl<boost::archive::binary_iarchive, char, std::char_traits<char> >::binary_iarchive_impl(std::istream&, unsigned int)':
ObjContainer.cpp:(.text._ZN5boost7archive20binary_iarchive_implINS0_15binary_iarchiveEcSt11char_traitsIcEEC2ERSij[_ZN5boost7archive20binary_iarchive_implINS0_15binary_iarchiveEcSt11char_traitsIcEEC5ERSij]+0x55): undefined reference to `boost::archive::basic_binary_iprimitive<boost::archive::binary_iarchive, char, std::char_traits<char> >::basic_binary_iprimitive(std::basic_streambuf<char, std::char_traits<char> >&, bool)'
ObjContainer.cpp:(.text._ZN5boost7archive20binary_iarchive_implINS0_15binary_iarchiveEcSt11char_traitsIcEEC2ERSij[_ZN5boost7archive20binary_iarchive_implINS0_15binary_iarchiveEcSt11char_traitsIcEEC5ERSij]+0xab): undefined reference to `boost::archive::basic_binary_iprimitive<boost::archive::binary_iarchive, char, std::char_traits<char> >::~basic_binary_iprimitive()'
/tmp/ccqY0aGT.o: In function `boost::archive::binary_oarchive_impl<boost::archive::binary_oarchive, char, std::char_traits<char> >::init(unsigned int)':
ObjContainer.cpp:(.text._ZN5boost7archive20binary_oarchive_implINS0_15binary_oarchiveEcSt11char_traitsIcEE4initEj[_ZN5boost7archive20binary_oarchive_implINS0_15binary_oarchiveEcSt11char_traitsIcEE4initEj]+0x21): undefined reference to `boost::archive::basic_binary_oarchive<boost::archive::binary_oarchive>::init()'
The boost::archive class is part of Boost.Serialization library.
You need to link with -lboost_serialization -lboost_system at the end.
I'm just learning C++ and I'm trying to compile a very simple program.
I've tried looking around the web and I have no idea what's wrong or why the error is saying that I have undefined references to various variables.
I'm using the g++ compiler on Linux.
Here is the code:
#include <iostream>
using namespace std;
int main()
{
double celsius_temp, fahrenheit_temp;
cout << "Input celsius temp:";
cin >> celsius_temp;
fahrenheit_temp = (celsius_temp * 1.8) + 32;
cout << "Fahrenheit temp is:" << fahrenheit_temp;
return 0;
}
Here was the command entered and the error:
$ gcc convert.cpp -o convert
/tmp/ccTjTVCH.o: In function `main':
convert.cpp:(.text+0x1d): undefined reference to `std::cout'
convert.cpp:(.text+0x22): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
convert.cpp:(.text+0x2e): undefined reference to `std::cin'
convert.cpp:(.text+0x33): undefined reference to `std::istream::operator>>(double&)'
convert.cpp:(.text+0x5f): undefined reference to `std::cout'
convert.cpp:(.text+0x64): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
convert.cpp:(.text+0x7c): undefined reference to `std::ostream::operator<<(double)'
/tmp/ccTjTVCH.o: In function `__static_initialization_and_destruction_0(int, int)':
convert.cpp:(.text+0xbe): undefined reference to `std::ios_base::Init::Init()'
convert.cpp:(.text+0xcd): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
Compile usingg++ instead of gcc. The errors are linker errors(It cannot find the function references). gcc can also compile cpp but it is complicated
I'd like to use Apache log4cxx and am running into a problem. I'm running on Debian 8 and installed log4cxx using
apt-get install liblog4cxx10-dev
I have embedded one of the code snippets in the Documentation Page into the code below:
#include <log4cxx/logger.h>
#include <math.h>
int main(int argc, char* argv[]) {
log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("com.foo"));
int i = 10;
const char* region = "World";
LOG4CXX_INFO(logger, "Simple message text.")
LOG4CXX_INFO(logger, "Hello, " << region)
LOG4CXX_DEBUG(logger, L"Iteration " << i)
LOG4CXX_DEBUG(logger, "e^10 = " << std::scientific << exp(10.0))
LOG4CXX_WARN(logger, L"" << i << L" is the number of the iteration.")
return 0;
}
When compiling with Eclipse CDT (gcc 5.3.0 built from source) I get the following linker errors:
make all
Building file: ../src/tester.cxx
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/tester.d" -MT"src/tester.o" -o "src/tester.o" "../src/tester.cxx"
Finished building: ../src/tester.cxx
Building target: logtest
Invoking: GCC C++ Linker
g++ -L/usr/lib/x86_64-linux-gnu -o "logtest" ./src/tester.o -llog4cxx
./src/tester.o: In function `main':
/home/andand/workspace/logtest/Debug/../src/tester.cxx:9: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](log4cxx::helpers::CharMessageBuffer&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:9: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:10: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](log4cxx::helpers::CharMessageBuffer&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:10: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:11: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](std::basic_ostream<wchar_t, std::char_traits<wchar_t> >&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:11: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:12: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](std::ostream&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:12: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:13: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](std::basic_ostream<wchar_t, std::char_traits<wchar_t> >&)'
makefile:45: recipe for target 'logtest' failed
/home/andand/workspace/logtest/Debug/../src/tester.cxx:13: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, log4cxx::spi::LocationInfo const&) const'
collect2: error: ld returned 1 exit status
make: *** [logtest] Error 1
I double checked where apt installed the libraries:
root#starsim-dev:/home/andand# find / -name *log4cxx*
/usr/lib/x86_64-linux-gnu/liblog4cxx.a
/usr/lib/x86_64-linux-gnu/liblog4cxx.so.10.0.0
/usr/lib/x86_64-linux-gnu/liblog4cxx.so.10
/usr/lib/x86_64-linux-gnu/liblog4cxx.so
/usr/lib/x86_64-linux-gnu/pkgconfig/liblog4cxx.pc
/usr/include/log4cxx
/usr/include/log4cxx/private/log4cxx_private.h
/usr/include/log4cxx/log4cxx.h
/usr/share/doc/liblog4cxx10
/usr/share/doc/liblog4cxx10-dev
/usr/share/lintian/overrides/liblog4cxx10
/usr/share/lintian/overrides/liblog4cxx10-dev
/var/cache/apt/archives/liblog4cxx10-dev_0.10.0-4_amd64.deb
/var/cache/apt/archives/liblog4cxx10_0.10.0-4_amd64.deb
/var/lib/dpkg/info/liblog4cxx10:amd64.postinst
/var/lib/dpkg/info/liblog4cxx10:amd64.shlibs
/var/lib/dpkg/info/liblog4cxx10-dev.list
/var/lib/dpkg/info/liblog4cxx10:amd64.list
/var/lib/dpkg/info/liblog4cxx10:amd64.md5sums
/var/lib/dpkg/info/liblog4cxx10:amd64.postrm
/var/lib/dpkg/info/liblog4cxx10-dev.md5sums
The documentation on Apache's website isn't entirely clear what other library dependencies exist. Does anybody have some insight into what I'm missing?
I ran into the same set of linker errors trying to use log4cxx on Ubuntu 18.04. I was able to get their code snippets to work - including the one above - by adding the following additional Apache libs to my MakeFile
LIBS+= -llog4cxx -laprutil-1 -lapr-1
Make sure to list them in the above order.
I am trying to compile the Contraction Hierarchies Implementation by KIT.
This software has been published in 2009 and obviously not been maintained since then. Since a few things have changed in the mean time (with new C++ standards and compiler versions) the code does not compile off the shelf anymore.
README is not very verbose when it comes to instructions on compiling and says you should only invoke make. However, using make gives me the following error:
g++ -Wall -W -Wno-unused-parameter -O6 -c -o main.o main.cpp
In file included from /usr/include/c++/4.8/ext/hash_map:60:0,
from command/../io/../datastr/graph/../../io/serialize.h:26,
from command/../io/../datastr/graph/edge.h:26,
from command/../io/../datastr/graph/graph.h:59,
from command/../io/createGraph.h:28,
from command/NodeOrder.h:29,
from main.cpp:35:
/usr/include/c++/4.8/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp]
#warning \
^
command/../processing/../EliminationWeight.h:42:35: error: a call to a constructor cannot appear in a constant-expression
static const Type MAX_VALUE = __DBL_MAX__;
^
command/../processing/../EliminationWeight.h:43:36: error: a call to a constructor cannot appear in a constant-expression
static const Type MIN_VALUE = -__DBL_MAX__;
^
make: *** [main.o] Error 1
What do I have to do to make this compile?
To make the software compile without errors, a few steps are necessary. I have tested these instruction with gcc 4.8.1 and Boost 1.53.0.
a call to a constructor cannot appear in a constant-expression
(I have already answered this subproblem in detail there but will repeat the important steps here.)
The first compilation error we encounter is due to the software using non-standard extensions of the GCC compiler. It seems that at the time of the software's development these did not have to be enabled by a compiler switch but modern GCC requires this. However, for future compatibility, we will just get rid of them and implement the features in a standard compliant way.
The compiler tells us that the problem is in lines 42 and 43 of EliminationWeight.h. These are the lines in that file:
static const Type MAX_VALUE = __DBL_MAX__;
static const Type MIN_VALUE = -__DBL_MAX__;
GCC complains here because initialization of doubles in a class body is not covered by the standard. Instead, we should do it separately. So remove the initialization and change the lines to the following:
static const Type MAX_VALUE;
static const Type MIN_VALUE;
Also remove the #include <limits> as we no longer need that in this file.
Since we still want to initialize these values, we take a look at main.cpp and find line 84 and following:
// doesn't look nice, but required by the compiler (gcc 4)
const EdgeWeight Weight::MAX_VALUE;
const EliminationWeight::Type EliminationWeight::MAX_VALUE;
const EliminationWeight::Type EliminationWeight::MIN_VALUE;
This looks like the perfect place to do our initializations. Change lines 86 and 87 to contain the following code:
const EliminationWeight::Type EliminationWeight::MAX_VALUE = std::numeric_limits< EliminationWeight::Type >::max();
const EliminationWeight::Type EliminationWeight::MIN_VALUE = -std::numeric_limits< EliminationWeight::Type >::max();
Also add
#include <limits>
to main.cpp because we are now using the std::numeric_limits class from that header.
collect2: error: ld returned 1 exit status
Compilation will now work but linking will fail with a huge error message. (I have already covered this problem there but will repeat the gist of it here.)
All the errors will be about linking of Boost::Regex. This is the complete error message on my machine:
g++ -lboost_regex -lboost_iostreams -o main main.o
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::unwind_extra_block(bool)':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb]+0x2c): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.o: In function `void boost::re_detail::raise_error > > >(boost::regex_traits_wrapper > > const&, boost::regex_constants::error_type)':
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0x7d): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xb1): undefined reference to `boost::re_detail::raise_runtime_error(std::runtime_error const&)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xcb): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.o: In function `__gnu_cxx::__normal_iterator boost::re_detail::re_is_set_member, char, boost::regex_traits >, unsigned int>(__gnu_cxx::__normal_iterator, __gnu_cxx::__normal_iterator, boost::re_detail::re_set_long const*, boost::re_detail::regex_data > > const&, bool)':
main.cpp:(.text._ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb[_ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb]+0x17b): undefined reference to `boost::re_detail::cpp_regex_traits_implementation::transform_primary(char const*, char const*) const'
main.cpp:(.text._ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb[_ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb]+0x4c0): undefined reference to `boost::re_detail::cpp_regex_traits_implementation::transform(char const*, char const*) const'
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::extend_stack()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE12extend_stackEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE12extend_stackEv]+0x18): undefined reference to `boost::re_detail::get_mem_block()'
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::match_imp()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0xc): undefined reference to `boost::re_detail::get_mem_block()'
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0x19e): undefined reference to `boost::re_detail::verify_options(unsigned int, boost::regex_constants::_match_flags)'
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0x254): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0x3c6): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.o: In function `bool boost::regex_match, std::allocator, std::allocator > >, char, boost::regex_traits > >(std::basic_string, std::allocator > const&, boost::match_results, std::allocator >::const_iterator, std::allocator > > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags)':
main.cpp:(.text._ZN5boost11regex_matchISt11char_traitsIcESaIcESaINS_9sub_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEEEEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbRKSbIT2_T_T0_ERNS_13match_resultsINSJ_14const_iteratorET1_EERKNS_11basic_regexISG_T3_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchISt11char_traitsIcESaIcESaINS_9sub_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEEEEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbRKSbIT2_T_T0_ERNS_13match_resultsINSJ_14const_iteratorET1_EERKNS_11basic_regexISG_T3_EENS_15regex_constants12_match_flagsE]+0xe9): undefined reference to `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::construct_init(boost::basic_regex > > const&, boost::regex_constants::_match_flags)'
main.o: In function `void Command::createVector(std::string const&, std::vector >&, double)':
main.cpp:(.text._ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x4a): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.cpp:(.text._ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x7b): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.cpp:(.text._ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0xac): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.o: In function `void Command::createVector(std::string const&, std::vector >&, unsigned int)':
main.cpp:(.text._ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x48): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.cpp:(.text._ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x79): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.o:main.cpp:(.text._ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0xaa): more undefined references to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)' follow
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::match_match()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE11match_matchEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE11match_matchEv]+0x371): undefined reference to `boost::match_results, std::allocator > > >::maybe_assign(boost::match_results, std::allocator > > > const&)'
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::match_dot_repeat_slow()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE21match_dot_repeat_slowEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE21match_dot_repeat_slowEv]+0x229): undefined reference to `boost::re_detail::get_mem_block()'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
It seems like Boost::Regex is not found at all. Note that this is one of the Boost libraries that need to be compiled and linked into your application to work. The following is under the assumption that you have obtained a compiled version and it is in your system's library directory.
When linking, the order in which libraries and object files are passed to GCC via command line parameters is important. For some reason what seems to have worked with GCC in 2009 is no longer working now. This can be fixed by changing the order in which make is passing the parameters to GCC.
Locate Makefile in the project root directory and find line 6:
$(CXX) $(LINK) -o $# $^ $(LIBS)
Here you can see that make is passing the linker switches containing the Boost libraries before the object file. (If you can't, don't worry. It is not necessary that you understand Makefiles to follow this explanation.) To make this work with current GCC we will change the order of the parameters. Line 6 of your Makefile should look like this:
$(CXX) -o $# $^ $(LIBS) $(LINK)
Save it and run make again. Compilation and linking should now proceed without errors.
warning: #warning This file includes at least one deprecated or antiquated header
You will still see a compiler warning telling you that you are using deprecated header files. You can ignore this warning. However, it might be possible that newer versions of GCC don't come with this header file and compilation will fail.
If you want to fix it, here is how.
The problem lies in io/serialize.h which includes <ext/hash_map>. The C++11 standard replaces this with unordered_map. So we fix the code to use that. Line 128 will be
typedef __gnu_cxx::hash_map<key_type, data_type> HashMap;
Change this to use unordered_map as follows:
typedef std::unordered_map<key_type, data_type> HashMap;
Now we also need to fix the header. Remove the include of <ext/hash_map> and replace line 26 with
#include <algorithm>
#include <unordered_map>
The <algorithm> include is needed because obviously <ext/hash_map> also provided std::sort and std::reverse for the hash_map. These functions are now in the <algorithm> header and the code relies on the fact that these are included by this file.
Since this is a C++11 feature we need to tell GCC that we want support for that. Go to compiler.make in the project's root directory and find line 6 which should be
CXXFLAGS = $(DEBUG) $(WARNING) $(OPTIMIZER)
At the end of the line add the switch to use C++11:
CXXFLAGS = $(DEBUG) $(WARNING) $(OPTIMIZER) -std=c++11
Run make clean and make again and your code should compile without any errors or warnings.
Newbie question... I'm trying out Boost for the first time because I want to test drive the Boost Log library. I built this test program...
#include <boost/log/trivial.hpp>
#include <iostream>
int fibonacci(int num) {
int i;
int a = 1;
int b = 1;
for (i = 2; i <= num; ++i) {
BOOST_LOG_TRIVIAL(info) << "Iteration " << i << " (a = " << a << ", b = " << b << ")...";
b = a + b;
a = b - a;
}
return a;
}
int main() {
std::cout << "8th fibonacci number: " << fibonacci(8) << std::endl;
return 0;
}
Compile data:
**** Build of configuration Debug for project LoggingCpp ****
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -lpthread -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: LoggingCpp
Invoking: GCC C++ Linker
g++ -lpthread -o"LoggingCpp" ./main.o
./main.o: In function `~basic_logger':
/usr/include/boost/log/sources/basic_logger.hpp:90: undefined reference to `boost::log_mt_posix::basic_attribute_set<char>::~basic_attribute_set()'
./main.o: In function `boost::log_mt_posix::trivial::logger::construct_logger()':
/usr/include/boost/log/trivial.hpp:102: undefined reference to `boost::log_mt_posix::trivial::aux::init()'
./main.o: In function `void boost::call_once<void (*)()>(boost::once_flag&, void (*)())':
/usr/include/boost/thread/pthread/once.hpp:51: undefined reference to `boost::detail::get_once_per_thread_epoch()'
/usr/include/boost/thread/pthread/once.hpp:55: undefined reference to `boost::detail::once_epoch_mutex'
/usr/include/boost/thread/pthread/once.hpp:66: undefined reference to `boost::detail::once_epoch_mutex'
/usr/include/boost/thread/pthread/once.hpp:77: undefined reference to `boost::detail::once_global_epoch'
/usr/include/boost/thread/pthread/once.hpp:77: undefined reference to `boost::detail::once_global_epoch'
/usr/include/boost/thread/pthread/once.hpp:77: undefined reference to `boost::detail::once_global_epoch'
/usr/include/boost/thread/pthread/once.hpp:78: undefined reference to `boost::detail::once_epoch_cv'
/usr/include/boost/thread/pthread/once.hpp:84: undefined reference to `boost::detail::once_epoch_mutex'
/usr/include/boost/thread/pthread/once.hpp:84: undefined reference to `boost::detail::once_epoch_cv'
/usr/include/boost/thread/pthread/once.hpp:88: undefined reference to `boost::detail::once_global_epoch'
/usr/include/boost/thread/pthread/once.hpp:73: undefined reference to `boost::detail::once_epoch_cv'
./main.o: In function `record_pump':
/usr/include/boost/log/sources/record_ostream.hpp:293: undefined reference to `boost::log_mt_posix::aux::stream_provider<char>::allocate_compound(boost::log_mt_posix::basic_record<char> const&)'
./main.o: In function `~auto_release':
/usr/include/boost/log/sources/record_ostream.hpp:280: undefined reference to `boost::log_mt_posix::aux::stream_provider<char>::release_compound(boost::log_mt_posix::aux::stream_provider<char>::stream_compound*)'
./main.o: In function `boost::log_mt_posix::sources::aux::logger_singleton<boost::log_mt_posix::trivial::logger>::init_instance()':
/usr/include/boost/log/sources/global_logger_storage.hpp:126: undefined reference to `boost::log_mt_posix::sources::aux::global_storage<char>::get_or_init(std::type_info const&, boost::function0<boost::shared_ptr<boost::log_mt_posix::sources::aux::logger_holder_base> > const&)'
/usr/include/boost/log/sources/global_logger_storage.hpp:147: undefined reference to `boost::log_mt_posix::odr_violation::throw_(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
./main.o: In function `boost::log_mt_posix::sources::aux::severity_level<boost::log_mt_posix::trivial::severity_level>::set_value(boost::log_mt_posix::trivial::severity_level)':
/usr/include/boost/log/sources/severity_feature.hpp:95: undefined reference to `boost::log_mt_posix::sources::aux::set_severity_level(int)'
./main.o: In function `boost::log_mt_posix::basic_record<char> boost::log_mt_posix::sources::basic_logger<char, boost::log_mt_posix::sources::severity_logger_mt<boost::log_mt_posix::trivial::severity_level>, boost::log_mt_posix::sources::multi_thread_model<boost::log_mt_posix::aux::light_rw_mutex> >::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log_mt_posix::keywords::tag::severity, boost::log_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log_mt_posix::keywords::tag::severity, boost::log_mt_posix::trivial::severity_level const> const&)':
/usr/include/boost/log/sources/basic_logger.hpp:269: undefined reference to `boost::log_mt_posix::basic_core<char>::open_record(boost::log_mt_posix::basic_attribute_set<char> const&)'
./main.o: In function `boost::log_mt_posix::sources::basic_logger<char, boost::log_mt_posix::sources::severity_logger_mt<boost::log_mt_posix::trivial::severity_level>, boost::log_mt_posix::sources::multi_thread_model<boost::log_mt_posix::aux::light_rw_mutex> >::push_record_unlocked(boost::log_mt_posix::basic_record<char> const&)':
/usr/include/boost/log/sources/basic_logger.hpp:280: undefined reference to `boost::log_mt_posix::basic_core<char>::push_record(boost::log_mt_posix::basic_record<char> const&)'
./main.o: In function `basic_logger<boost::parameter::aux::tagged_argument<boost::log_mt_posix::keywords::tag::severity, const boost::log_mt_posix::trivial::severity_level> >':
/usr/include/boost/log/sources/basic_logger.hpp:145: undefined reference to `boost::log_mt_posix::basic_core<char>::get()'
/usr/include/boost/log/sources/basic_logger.hpp:145: undefined reference to `boost::log_mt_posix::basic_attribute_set<char>::basic_attribute_set()'
./main.o: In function `~pair':
/usr/include/c++/4.5/bits/stl_pair.h:72: undefined reference to `boost::log_mt_posix::basic_slim_string<char, std::char_traits<char> >::~basic_slim_string()'
/usr/include/c++/4.5/bits/stl_pair.h:72: undefined reference to `boost::log_mt_posix::basic_slim_string<char, std::char_traits<char> >::~basic_slim_string()'
./main.o: In function `basic_logger':
/usr/include/boost/log/sources/basic_logger.hpp:135: undefined reference to `boost::log_mt_posix::basic_core<char>::get()'
/usr/include/boost/log/sources/basic_logger.hpp:135: undefined reference to `boost::log_mt_posix::basic_attribute_set<char>::basic_attribute_set(boost::log_mt_posix::basic_attribute_set<char> const&)'
./main.o: In function `boost::log_mt_posix::basic_attribute_set<char>::reference_proxy::operator=(boost::shared_ptr<boost::log_mt_posix::attribute> const&) const':
/usr/include/boost/log/attributes/attribute_set.hpp:121: undefined reference to `boost::log_mt_posix::basic_slim_string<char, std::char_traits<char> >::basic_slim_string(char const*, unsigned long)'
/usr/include/boost/log/attributes/attribute_set.hpp:121: undefined reference to `boost::log_mt_posix::basic_attribute_set<char>::insert(boost::log_mt_posix::basic_slim_string<char, std::char_traits<char> > const&, boost::shared_ptr<boost::log_mt_posix::attribute> const&)'
/usr/include/boost/log/attributes/attribute_set.hpp:121: undefined reference to `boost::log_mt_posix::basic_slim_string<char, std::char_traits<char> >::~basic_slim_string()'
/usr/include/boost/log/attributes/attribute_set.hpp:121: undefined reference to `boost::log_mt_posix::basic_slim_string<char, std::char_traits<char> >::~basic_slim_string()'
./main.o: In function `pair<std::basic_string<char>, boost::shared_ptr<boost::log_mt_posix::attribute> >':
/usr/include/c++/4.5/bits/stl_pair.h:116: undefined reference to `boost::log_mt_posix::basic_slim_string<char, std::char_traits<char> >::basic_slim_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
./main.o: In function `boost::log_mt_posix::basic_attribute_set<char>::insert(std::pair<boost::log_mt_posix::basic_slim_string<char, std::char_traits<char> > const, boost::shared_ptr<boost::log_mt_posix::attribute> > const&)':
/usr/include/boost/log/attributes/attribute_set.hpp:507: undefined reference to `boost::log_mt_posix::basic_attribute_set<char>::insert(boost::log_mt_posix::basic_slim_string<char, std::char_traits<char> > const&, boost::shared_ptr<boost::log_mt_posix::attribute> const&)'
./main.o: In function `boost::log_mt_posix::sources::aux::severity_level<boost::log_mt_posix::trivial::severity_level>::dispatch(boost::log_mt_posix::type_dispatcher&)':
/usr/include/boost/log/sources/severity_feature.hpp:105: undefined reference to `boost::log_mt_posix::sources::aux::get_severity_level()'
./main.o: In function `boost::log_mt_posix::sources::aux::severity_level<boost::log_mt_posix::trivial::severity_level>::detach_from_thread()':
/usr/include/boost/log/sources/severity_feature.hpp:118: undefined reference to `boost::log_mt_posix::sources::aux::get_severity_level()'
collect2: ld returned 1 exit status
make: *** [LoggingCpp] Error 1
About Boost Log syntax
Did I install Boost Log incorrectly? Am I missing crucial libraries? Did I omit necessary linker flags?
You need to add something like -lboost-log-mt to your link line.
It looks like you haven't built log?
Once you've copied the log header files, etc into the corresponding folders in your boost distro (keep the same structure i.e. merge the headers to the headers, the libs folder into the libs folder of your boost distro, etc), you still need to build boost and then like John mentioned, link against it. That's why the link you referred to above points you to the standard boost build instructions.
It's a while ago, but AFAIR on linux you simply run ./bjam in the root of your boost folder.
Maybe switch unright version of the boost_log.
I have the same issue for undefined reference to "boost::log::v2_mt_posix::core::push_record_move(boost::log::v2_mt_posix::record&)"
I build the libboost_log by myself. And I use the nm to grep the push_record_move found that, "boost::log::v2s_mt_posix::core::push_record_move(boost::log::v2s_mt_posix::record&)"
From the experience of before, should add some cflag in the Makefile.