【问题标题】:How to link my console app to boost libraries present in /usr/local/lib on an OSX environment with clang++ as compiler如何将我的控制台应用程序链接到使用 clang++ 作为编译器的 OSX 环境中的 /usr/local/lib 中存在的库
【发布时间】:2018-03-09 19:37:11
【问题描述】:

我在 server.cpp 文件中有一个简单的 boost asio 服务器控制台应用程序,该文件是从 boost official example 提取的。我在安装了 clangMacOS Sierra 上运行它。

server.cpp

#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>

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

std::string make_daytime_string() {

  using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  return ctime(&now);
}

int main() {

    try {
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1203));

        for (;;) {

            tcp::socket socket(io_service);
            acceptor.accept(socket);

            std::string message = make_daytime_string();

            boost::system::error_code ignored_error;
            boost::asio::write(socket, boost::asio::buffer(message),
            boost::asio::transfer_all(), ignored_error);
        }
    }
    catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

我正在尝试用 clang 编译它,使用以下编译命令

clang++ server.cpp -o server

但我收到以下错误

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

问题:
我可以理解它无法链接我位于 /usr/local/lib 的 boost 库。如何确保此程序链接到 /usr/local/lib 中可用的 boost 库和 /usr/local/include/boost 中可用的 boost 包含?

Clang 版本:
我的 clang 版本在终端运行 clang -v 后显示如下。

Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

注意:
这个问题与控制台应用程序的一般链接器问题无关。这个问题对于 boost 来说非常具体

【问题讨论】:

标签: c++ boost clang


【解决方案1】:

你应该在你的编译命令中添加-lboost_system,它会告诉链接器链接库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 2013-07-21
    • 2013-07-27
    • 2011-06-02
    • 2022-12-15
    • 2011-01-30
    • 1970-01-01
    • 2011-06-15
    相关资源
    最近更新 更多