首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在CPP应用程序中使用CPR库打开包含cpr/cprver.h文件

无法在CPP应用程序中使用CPR库打开包含cpr/cprver.h文件
EN

Stack Overflow用户
提问于 2022-04-25 10:56:37
回答 1查看 502关注 0票数 0

你好,我在cpp应用程序中使用CPR库,请求http请求到我的api,但是在添加额外的包含目录后,我会收到错误,比如“无法打开包含文件cpr/cprver.h”。

正如我所检查的,cpr文件夹中没有名称为cprver.h的文件。

我所做的:

  • https://github.com/whoshuu/cpr下载CPR库
  • 使用额外的包含目录在cpp项目中添加库:我给出了一个路径,直到D:\cpr-master\include。
代码语言:javascript
运行
复制
#include <cpr/cpr.h>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {
    cpr::Response r = cpr::Get(
        cpr::Url{ "https://jsonplaceholder.typicode.com/todos/1" });

    cout << r.status_code << "\n";            // 200
    cout << r.header["content-type"] << "\n"; // application/json;charset=utf-8
    cout << r.text << "\n";                   // JSON text string

    return 0;
}
error: can not open include file cpr/cprver.h.
There is no error in code.
Hope i explain well. Thank you in advance.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-25 11:36:03

我认为仅仅从Github下载cpr库是不够的,您应该根据二进制文件构建并链接cpr

根据文档,有几种使用cpr的方法

Cmake

如果您已经有了一个需要集成CMake请求的C++项目,主要的方法是使用fetch_content。将以下内容添加到CMakeLists.txt中。

代码语言:javascript
运行
复制
include(FetchContent)
FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git
                         GIT_TAG 6ea2dec23c3df14ac3b27b7d2d6bbff0cb7ba1b0) # The commit hash for 1.8.1. Replace with the latest from: https://github.com/libcpr/cpr/releases
FetchContent_MakeAvailable(cpr)

这将产生目标cpr::cpr,您可以将其链接到典型的方式:

代码语言:javascript
运行
复制
target_link_libraries(your_target_name PRIVATE cpr::cpr)

这样就行了!没必要自己处理胡言乱语。所有依赖项都会为您处理。所有这些都可以在这里的一个例子中找到。

如果您想在不同的平台上构建您的应用程序,我认为Cmake是正确的方法。

vcpkg

由于您正在使用Windows,所以我认为vcpkg是一种简单的方法:

您可以使用vcpkg依赖项管理器下载和安装cpr:

代码语言:javascript
运行
复制
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install cpr

微软团队成员和社区贡献者更新了vcpkg中的cpr端口。如果版本过期,请在vcpkg存储库上创建问题或请求。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71998434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档