C++ 编译 boost.asio 示例时出现链接器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2568243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:56:25  来源:igfitidea点击:

Linker error when compiling boost.asio example

c++compiler-constructionlinkerg++boost-asio

提问by Alon Gubkin

I'm trying to learn a little bit C++ and Boost.Asio. I'm trying to compile the following code example:

我正在尝试学习一点 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;
}

With the following command line:

使用以下命令行:

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

and it throws an unclear error:

它引发了一个不清楚的错误:

/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

How can I fix it?

我该如何解决?

回答by sisis

you need to link against libboost_system and apparently also against libboost_thread.

您需要链接到 libboost_system,显然还需要链接到 libboost_thread。

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

in case of multi-threaded libraries:

如果是多线程库:

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

回答by Timo Geusch

Have you built the boost libraries or are you trying to do this with a header-only setup? The error looks like the boost_system library is missing, which would suggest that the linker can't find the pre-built library.

您是否构建了 boost 库,或者您是否尝试使用仅标头设置来执行此操作?该错误看起来像是缺少 boost_system 库,这表明链接器找不到预构建的库。

回答by yeeking

You need to tell g++ where the header files are, where the libraries are and which library you are using, e.g. on my system:

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

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

where -I tells g++ where the header files are and -L tells g++ where the actual libraries are and -lboost_thread-mt is the library I want to link against in the -L folder.

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