你好,我在cpp应用程序中使用CPR库,请求http请求到我的api,但是在添加额外的包含目录后,我会收到错误,比如“无法打开包含文件cpr/cprver.h”。
正如我所检查的,cpr文件夹中没有名称为cprver.h的文件。
我所做的:
#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.
发布于 2022-04-25 11:36:03
我认为仅仅从Github下载cpr
库是不够的,您应该根据二进制文件构建并链接cpr
。
根据文档,有几种使用cpr
的方法
Cmake
如果您已经有了一个需要集成CMake请求的C++项目,主要的方法是使用
fetch_content
。将以下内容添加到CMakeLists.txt中。
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
,您可以将其链接到典型的方式:
target_link_libraries(your_target_name PRIVATE cpr::cpr)
这样就行了!没必要自己处理胡言乱语。所有依赖项都会为您处理。所有这些都可以在这里的一个例子中找到。
如果您想在不同的平台上构建您的应用程序,我认为Cmake
是正确的方法。
vcpkg
由于您正在使用Windows,所以我认为vcpkg
是一种简单的方法:
您可以使用vcpkg依赖项管理器下载和安装cpr:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install cpr
微软团队成员和社区贡献者更新了vcpkg中的cpr
端口。如果版本过期,请在vcpkg存储库上创建问题或请求。
https://stackoverflow.com/questions/71998434
复制相似问题