Getting unexpected "undefined reference to" errors when compiling with a static library

2.3k views Asked by At

I've been trying to make a static library out of this: https://github.com/onlinecity/cpp-smpp , as I'd like to make a single file executable that uses it.

So far I have gone into src/smpp/ and created the .o files with:

g++-5 -c *.cpp -std=c++11 -I.

Then made them into an .a file with:

ar rvs libsmpp.a *.o

Then I'm trying to compile a small main.cpp file that uses the library with this:

g++-5 main.cpp -I/usr/local/include -static libsmpp.a -lboost_system -lboost_regex -lboost_date_time -lpthread -lm -lglog -lgflags -std=c++11

And I get some undefined reference errors:

/usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.a(condition_variable.o): In function `std::condition_variable::~condition_variable()':
(.text._ZNSt18condition_variableD2Ev+0x1): undefined reference to `pthread_cond_destroy'
/usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.a(condition_variable.o): In function `std::condition_variable::wait(std::unique_lock<std::mutex>&)':
(.text._ZNSt18condition_variable4waitERSt11unique_lockISt5mutexE+0x8): undefined reference to `pthread_cond_wait'
libsmpp.a(smppclient.o): In function `smpp::SmppClient::sendPdu(smpp::PDU&)':
smppclient.cpp:(.text+0x22a1): undefined reference to `google::LogMessage::LogMessage(char const*, int)'
smppclient.cpp:(.text+0x22b0): undefined reference to `google::LogMessage::stream()'
smppclient.cpp:(.text+0x22d4): undefined reference to `google::LogMessage::~LogMessage()'
smppclient.cpp:(.text+0x2534): undefined reference to `google::LogMessage::~LogMessage()'
libsmpp.a(smppclient.o): In function `smpp::SmppClient::readPdu(bool const&)':
smppclient.cpp:(.text+0x29d8): undefined reference to `google::LogMessage::LogMessage(char const*, int)'
smppclient.cpp:(.text+0x29e7): undefined reference to `google::LogMessage::stream()'
smppclient.cpp:(.text+0x2a0b): undefined reference to `google::LogMessage::~LogMessage()'
smppclient.cpp:(.text+0x2a48): undefined reference to `google::LogMessage::~LogMessage()'
libsmpp.a(smppclient.o): In function `boost::asio::detail::posix_event::~posix_event()':
smppclient.cpp:(.text._ZN5boost4asio6detail11posix_eventD2Ev[_ZN5boost4asio6detail11posix_eventD5Ev]+0x14): undefined reference to `pthread_cond_destroy'
libsmpp.a(smppclient.o): In function `boost::asio::detail::posix_event::posix_event()':
smppclient.cpp:(.text._ZN5boost4asio6detail11posix_eventC2Ev[_ZN5boost4asio6detail11posix_eventC5Ev]+0x2e): undefined reference to `pthread_cond_init'
libsmpp.a(smppclient.o): In function `void boost::asio::detail::posix_event::wait<boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex> >(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&)':
smppclient.cpp:(.text._ZN5boost4asio6detail11posix_event4waitINS1_11scoped_lockINS1_11posix_mutexEEEEEvRT_[_ZN5boost4asio6detail11posix_event4waitINS1_11scoped_lockINS1_11posix_mutexEEEEEvRT_]+0x5f): undefined reference to `pthread_cond_wait'
libsmpp.a(timeformat.o): In function `boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::iso_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::ostream&)':
timeformat.cpp:(.text._ZN5boost9date_time15month_formatterINS_9gregorian10greg_monthENS0_10iso_formatIcEEcE12format_monthERKS3_RSo[_ZN5boost9date_time15month_formatterINS_9gregorian10greg_monthENS0_10iso_formatIcEEcE12format_monthERKS3_RSo]+0x33): undefined reference to `boost::gregorian::greg_month::as_short_string() const'
timeformat.cpp:(.text._ZN5boost9date_time15month_formatterINS_9gregorian10greg_monthENS0_10iso_formatIcEEcE12format_monthERKS3_RSo[_ZN5boost9date_time15month_formatterINS_9gregorian10greg_monthENS0_10iso_formatIcEEcE12format_monthERKS3_RSo]+0x56): undefined reference to `boost::gregorian::greg_month::as_long_string() const'
collect2: error: ld returned 1 exit status

It compiles fine when I link to a dynamic version of the smpp library, although that's made using the cmake files included with the smpp lib. I didn't know how to modify them to output a static library, so I just used the g++ command at the top, and added the libraries listed in them (like glog) in the main.cpp compile command.

The "undefined reference to google::logMessage" seems especially strange, as I'm pretty sure thats part of glog, which is definitely loaded and included in the smppclient.cpp file itself.

Any ideas? Thanks.

0

There are 0 answers