编译boost.asio示例时链接器错误

时间:2010-04-02 17:51:47

标签: c++ compiler-construction linker g++ boost-asio

我正在尝试学习一点点C ++和Boost.Asio。我正在尝试编译以下代码示例:

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: client <host>" << std::endl;
      return 1;
    }

    boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(argv[1], "daytime");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
    tcp::resolver::iterator end;

    tcp::socket socket(io_service);
    boost::system::error_code error = boost::asio::error::host_not_found;
    while (error && endpoint_iterator != end)
    {
      socket.close();
      socket.connect(*endpoint_iterator++, error);
    }
    if (error)
      throw boost::system::system_error(error);

    for (;;)
    {
      boost::array<char, 128> buf;
      boost::system::error_code error;

      size_t len = socket.read_some(boost::asio::buffer(buf), error);

      if (error == boost::asio::error::eof)
        break; // Connection closed cleanly by peer.
      else if (error)
        throw boost::system::system_error(error); // Some other error.

      std::cout.write(buf.data(), len);
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

使用以下命令行:

g++ -I /usr/local/boost_1_42_0 a.cpp

它引发了一个不明确的错误:

/tmp/ccCv9ZJA.o: In function `__static_initialization_and_destruction_0(int, int)':
a.cpp:(.text+0x654): undefined reference to `boost::system::get_system_category()'
a.cpp:(.text+0x65e): undefined reference to `boost::system::get_generic_category()'
a.cpp:(.text+0x668): undefined reference to `boost::system::get_generic_category()'
a.cpp:(.text+0x672): undefined reference to `boost::system::get_generic_category()'
a.cpp:(.text+0x67c): undefined reference to `boost::system::get_system_category()'
/tmp/ccCv9ZJA.o: In function `boost::system::error_code::error_code()':
a.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x10): undefined reference to `boost::system::get_system_category()'
/tmp/ccCv9ZJA.o: In function `boost::asio::error::get_system_category()':
a.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[boost::asio::error::get_system_category()]+0x7): undefined reference to `boost::system::get_system_category()'
/tmp/ccCv9ZJA.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
a.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x1d): undefined reference to `pthread_detach'
/tmp/ccCv9ZJA.o: In function `boost::asio::detail::posix_thread::join()':
a.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[boost::asio::detail::posix_thread::join()]+0x25): undefined reference to `pthread_join'
/tmp/ccCv9ZJA.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::~posix_tss_ptr()':
a.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED5Ev]+0xf): undefined reference to `pthread_key_delete'
/tmp/ccCv9ZJA.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::posix_tss_ptr()':
a.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC5Ev]+0x22): undefined reference to `pthread_key_create'
collect2: ld returned 1 exit status

我该如何解决?

3 个答案:

答案 0 :(得分:20)

你需要链接libboost_system,显然还要反对libboost_thread。

g++ -I /usr/local/boost_1_42_0 -lboost_system -lboost_thread a.cpp

如果是多线程库:

g++ -I /usr/local/boost_1_42_0 -lboost_system-mt -lboost_thread-mt a.cpp

答案 1 :(得分:2)

您是否构建了增强库,或者您是否尝试使用仅限标头的设置?该错误看起来像是缺少boost_system库,这表明链接器无法找到预先构建的库。

答案 2 :(得分:1)

您需要告诉g ++头文件的位置,库的位置以及您使用的库,例如:在我的系统上:

g++  -I /opt/local/include -L /opt/local/lib -lboost_system-mt -lboost_thread-mt a.cpp

其中-I告诉g ++头文件的位置,-L告诉g ++实际库的位置,-lboost_thread-mt是我要在-L文件夹中链接的库。