I have simple boost asio server console app in a server.cpp file which is picked up as it is from boost official example. I am running it on MacOS Sierra with clang installed.
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;
}
I am trying to compile it with clang using the following compile command:
clang++ server.cpp -o server
But I get the following error:
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)
Question:
I can understand that it not able to link my boost libraries located in /usr/local/lib
. How can I ensure that this program links to boost libraries available in /usr/local/lib
and boost includes available in /usr/local/include/boost
?
Clang version:
My clang version shows following after running clang -v
in terminal.
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
Note:
This question is not about general linker issues of console apps. This question is very specific to boost