0

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

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). Specifically, see [this answer](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400). – Miles Budnek Mar 09 '18 at 00:25

1 Answers1

1

You should add -lboost_system to your compile command, it will tell the linker to link the library

phonzia
  • 66
  • 5