【发布时间】:2018-03-09 19:37:11
【问题描述】:
我在 server.cpp 文件中有一个简单的 boost 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 的 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 来说非常具体
【问题讨论】: