C++ cpp-net lib 未找到

标签 c++ linux boost cpp-netlib

这是一段来自 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 header 所在的包含路径。第一个错误行告诉您缺少哪个 header 。假设您的 Boost header 安装在/a/my_boost 下(即有一个/a/my_boost/boost header 子目录),cpp-netlib 安装在/a/my_cpp-netlib 下,您需要为编译器添加 -I 命令行选项:

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

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

关于C++ cpp-net lib 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29644790/

相关文章:

linux - 终端 Linux - 引用可执行文件 - 没有这样的文件或目录

c++ - 从静态库使用 Boost Asio 时出现访问冲突异常

linux - 使用 boost::iostreams::mapped_file

c++ - COM 自动化后 Excel 进程不会退出

c++ - 将类对象传递给线程

c++ - 两个运算符(operator)的一些奇怪冲突<<

linux - echo -e 可以在脚本中使用吗

linux - 如何从命令行检查 Mercurial 标签是否已经存在?

android - dlopen 用于 android 中的不同风格

c++ - 为什么 boost::shared_ptr 使用 gcc 内联汇编来增加 use_count 而不是使用 operator++?