Home > database >  can not open include file cpr/cprver.h using CPR library in CPP application
can not open include file cpr/cprver.h using CPR library in CPP application

Time:05-06

Hello i work with CPR library in cpp application for http request to my api but after i add additional include directory, i am getting error like 'can not open include file cpr/cprver.h'

As i check there is no file with the name cprver.h in cpr folder.

What i did:

  • Download CPR library from https://github.com/whoshuu/cpr
  • Add library in cpp project using additional include directory : i give a path till D:\cpr-master\include.
#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.

CodePudding user response:

I think simply downloading cpr library from Github is not enough, you should build and link cpr against your binary.

According to the documentation, there are several ways to use cpr:

Cmake

If you already have a CMake project you need to integrate C Requests with, the primary way is to use fetch_content. Add the following to your 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)

This will produce the target cpr::cpr which you can link against the typical way:

target_link_libraries(your_target_name PRIVATE cpr::cpr)

That should do it! There's no need to handle libcurl yourself. All dependencies are taken care of for you. All of this can be found in an example here.

If you want to build your application on different platform, I think Cmake is the proper way to go.

vcpkg

Since you are working on Windows, I think vcpkg is the easy way to go:

You can download and install cpr using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install cpr

The cpr port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.

  • Related