我在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包括?lang版本:
在终端中运行
clang -v
后,我的clang版本显示以下内容。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
注意:
此问题与控制台应用程序的一般链接程序问题无关。这个问题是非常具体的
最佳答案
您应该在编译命令中添加-lboost_system
,它将告诉链接器链接库