c++linuxboostcpp-netlib

C++ cpp-net lib not found


Here is a piece of code which is an example from cpp-netlib

#include <boost/network/protocol/http/server.hpp>
#include <string>
#include <iostream>

namespace http = boost::network::http;

struct hello_world;
typedef http::server<hello_world> server;

struct hello_world {
    void operator() (server::request const &request,
                     server::response &response) {
        std::string ip = source(request);
        response = server::response::stock_reply(
            server::response::ok, std::string("Hello, ") + ip + "!");
    }
};

int
main(int argc, char * argv[]) {

    if (argc != 3) {
        std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
        return 1;
    }

    try {
        hello_world handler;
        server server_(argv[1], argv[2], handler);
        server_.run();
    }
    catch (std::exception &e) {
        std::cerr << e.what() << std::endl;
        return 1;
    }

    return 0;
}

But on compiling that is g++ main.cpp -o socke.exe -lboost_system I get the following errors

main.cpp:1:50: error: boost/network/protocol/http/server.hpp: No such file or directory
main.cpp:5: error: âboostâ has not been declared

I have installed the cpnet-lib libraries and cmake for build them. I cant get to understand why couldnt the compiler find the libraries.


Solution

  • You didn't specify include path where Boost and cpp-netlib headers are located. The first error line tells which header is missing. Assuming your Boost headers are installed under /a/my_boost (i.e. there is a /a/my_boost/boost subdirectory with headers) and cpp-netlib under /a/my_cpp-netlib, you need to add -I command line options for your compiler:

    g++ main.cpp -o socke.exe -I/a/my_boost -I/a/my_cpp-netlib -lboost_system
    

    If you're using a graphical IDE or a build system, there should be an option in the project settings to add include directories.