C++ cpp-net lib not found



这是一段代码,它是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;
}

但是在编译时,g++ main.cpp -o socke.exe -lboost_system我收到以下错误

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

我已经安装了 cpnet-lib 库和 cmake 来构建它们。我无法理解为什么编译器找不到库。

您没有指定 Boost 和 cpp-netlib 标头所在的包含路径。第一个错误行指示缺少哪个标头。假设你的 Boost 标头安装在/a/my_boost 下(即有一个带有标头的/a/my_boost/boost 子目录)和/a/my_cpp-netlib 下的 cpp-netlib,你需要为编译器添加 -I 命令行选项:

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

如果您使用的是图形化 IDE 或构建系统,则项目设置中应该有一个用于添加包含目录的选项。

最新更新