如何链接我的控制台应用程序以使用clang ++作为编译器来提升OSX环境中/ usr / local / lib中的库

时间:2018-03-08 23:35:16

标签: c++ boost clang

我在 server.cpp 文件中有简单的提升asio服务器控制台应用,该文件是从boost official example获取的。我在安装了 clang MacOS 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的我的增强库。如何确保此程序链接到/usr/local/lib中可用的提升库以及/usr/local/include/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

注意:
此问题与控制台应用程序的常规链接器问题无关。这个问题非常具体,可以提升

1 个答案:

答案 0 :(得分:1)

您应该在编译命令中添加$computer,它会告诉链接器链接库