繁体   English   中英

如何链接到使用clang ++的/ usr / local编译中的boost?

[英]How to link to boost in /usr/local compiling with clang++?

我不是基于命令行的编译专家。 我开发了以下asio UDP应用程序,其代码来自boost官方示例

// 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)

尝试这个:

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

您将头文件包含为<boost/asio.hpp> ,因此只需传递-I /usr/local/include -I -L仅让链接器知道在何处查找标头和库。 您还需要让链接程序知道实际上需要通过-l<library_name>链接的库。

顺便说一句, /usr/local/include是默认的头搜索路径, /usr/local/lib是默认的库搜索路径。 因此,您可以:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM