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

前端之家收集整理的这篇文章主要介绍了c – 编译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

我该如何解决

解决方法

你需要链接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
原文链接:/c/115206.html

猜你在找的C&C++相关文章