Boost undefined references [duplicate]

2019-08-18 04:03发布

问题:

This question already has an answer here:

  • What is an undefined reference/unresolved external symbol error and how do I fix it? 33 answers

I'm trying to learn the boost library and I'm having trouble even compiling a simple program from their official tutorials.

Ubuntu 16.04LTS, installed boost via:

sudo apt-get install libboost-all-dev

test.cpp:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main()
{
    boost::asio::io_service io;
    boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
    t.wait();
    std::cout << "Hello, world!" << std::endl;

    return 0;
}

compile command:

g++ -l boost_system test.cpp
/tmp/ccFqTvqW.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x12b): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x137): undefined reference to `boost::system::system_category()'
/tmp/ccFqTvqW.o: In function `boost::system::error_code::error_code()':
test.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/ccFqTvqW.o: In function `boost::asio::error::get_system_category()':
test.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status

回答1:

Add these flags to your compilation command (g++ or clang):

-lboost_system -lboost_filesystem


标签: c++ ubuntu boost