【问题标题】:How to link to boost in /usr/local compiling with clang++?如何使用 clang++ 链接到 /usr/local 编译中的 boost?
【发布时间】:2017-05-06 12:35:42
【问题描述】:

我不是基于命令行的编译专家。我开发了下面的 asio UDP 应用程序,从boost official example 获取代码。

// udpServer.cpp

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

using boost::asio::ip::udp;

int main()
{
  try
  {
    boost::asio::io_service io_service;

    udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));

    for (;;)
    {
      std::array<char, 1> recv_buf;
      udp::endpoint remote_endpoint;
      boost::system::error_code error;
      socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint, 0, error);

      if (error && error != boost::asio::error::message_size)
        throw boost::system::system_error(error);

      std::string message = "some_string";

      boost::system::error_code ignored_error;
      socket.send_to(boost::asio::buffer(message), remote_endpoint, 0, ignored_error);
    }
  }

  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

我已经使用 Macports 安装了 boost 1.59,因为这是我得到的版本 sudo port install boost。我看到提升位于我的/usr/local/lib/usr/local/include 中的标题

我尝试了其他讨论的建议,但代码无法编译,因为它无法链接到 boost。我在 OSX 上并尝试使用以下命令使用 clang 进行编译

clang++ -std=c++14 udpServer.cpp

试过了

clang++ -std=c++14 -I /usr/local/include/boost -L /usr/local/lib udpServer.cpp

但出现以下错误:

Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
  boost::asio::error::get_system_category() in udpServer-4b9a12.o
  boost::system::error_code::error_code() in udpServer-4b9a12.o
  ___cxx_global_var_init.2 in udpServer-4b9a12.o
"boost::system::generic_category()", referenced from:
  ___cxx_global_var_init in udpServer-4b9a12.o
  ___cxx_global_var_init.1 in udpServer-4b9a12.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

【问题讨论】:

  • 为什么不使用homebrew

标签: c++ macos boost clang asio


【解决方案1】:

试试这个:

clang++ -std=c++14 -I /usr/local/include -L /usr/local/lib -lboost_system udpServer.cpp -o updServer

您将头文件包含为&lt;boost/asio.hpp&gt;,因此只需传递-I /usr/local/include-I -L 让链接器知道在哪里可以找到头文件和库。您还需要通过-l&lt;library_name&gt; 让链接器知道您实际需要链接的库。

顺便说一下,/usr/local/include 是默认的头文件搜索路径,/usr/local/lib 是默认的库搜索路径。所以你可以:

clang++ -std=c++14 -lboost_system udpServer.cpp -o updServer

【讨论】:

  • 非常感谢。这就是我需要的。现在了解这是如何工作的。是的
猜你喜欢
  • 2012-01-19
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多