cpp-httplib

安装
cpp-httplib gitee链接:

https://gitee.com/yuanfeng1897/cpp-httplib?_from=gitee_search

v0.7.15版本链接:

https://gitee.com/yuanfeng1897/cpp-httplib/tree/v0.7.15

httplib.h拷贝到我们的项目中即可

接入cpp-httplib:header-only,只需要将.h拷贝到你的项目中,即可直接使用
cpp-httplib:需要使用高版本的gcc,建议是gcc 7,8,9 [如果没有升级,cpp-httplib:要么就是编译报错,要么就是运行出错]

// 升级gcc
sudo yum install centos-release-scl scl-utils-build
sudo yum install -y devtoolset-7-gcc devtoolset-7-gcc-c++
ls /opt/rh/


cat ~/.bash_profile
## 添加下面这行
scl enable devtoolset-7 bash

关掉xshell重新登录即可

cpp-httplib: 阻塞式多线程的一个网络http库
所以在编译时要加上 -lpthread

如何使用cpp-httplib呢?

// 只需要这两行即可启动一个http服务
#include "../comm/httplib.h"

using namespace httplib;

int main()
{
    Server svr;
    svr.listen("0.0.0.0",8080,0); // 启动http服务
   
    return 0;
}

cpp-httplib_第1张图片

#include "../comm/httplib.h"

using namespace httplib;

int main()
{
    Server svr;
    // 当用户有请求 /hello 时,就给他返回后面的字符串
    svr.Get("/hello", [](const Request &req, Response &resp)
            {
                resp.set_content("Hello World!", "text/plain"); // 响应的文字内容,内容的类型
            });
    svr.set_base_dir("../wwwroot/index.html");
    svr.listen("0.0.0.0", 8080); // 启动http服务

    return 0;
}

更多的细节可以看gitee上面的readme使用手册!

你可能感兴趣的:(第三方开源库,cpp-httplib)