C ++编译cpr

时间:2019-12-30 20:53:52

标签: c++ cmake

我正在尝试使用CPR发送HTTP请求,但是我不能对其进行编译,因此我的应用程序的编译器返回:

1>Send.obj : error LNK2019: symbole externe non résolu "public: __cdecl cpr::Session::Session(void)" (??0Session@cpr@@QEAA@XZ) référencé dans la fonction "class cpr::Response __cdecl cpr::Get<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &&)" (??$Get@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@cpr@@YA?AVResponse@0@$$QEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

在github上,没有太多关于如何编译的文档,因此我尝试使用CMake,但它还会返回错误:

C++ Requests CMake Options
=======================================================
  USE_SYSTEM_CURL: OFF
  BUILD_CPR_TESTS: ON
  GENERATE_COVERAGE: OFF
  CPR_CURL_NOSIGNAL: OFF
  USE_SYSTEM_GTEST: OFF
  CMAKE_USE_OPENSSL: ON
=======================================================
Not using system Curl, using built-in curl project instead.
CMake Error at opt/CMakeLists.txt:48 (add_subdirectory):
  The source directory

    D:/Developpement/C++/cpr-master/opt/curl

  does not contain a CMakeLists.txt file.


CMake Error at opt/CMakeLists.txt:60 (set_property):
  set_property could not find TARGET libcurl.  Perhaps it has not yet been
  created.


Set CURL_FOUND to TRUE.
Set CURL_LIBRARIES to libcurl.
Set CURL_INCLUDE_DIRS to /include;/include/curl.
Not using system gtest, using built-in googletest project instead.
CMake Error at opt/CMakeLists.txt:82 (add_subdirectory):
  The source directory

    D:/Developpement/C++/cpr-master/opt/googletest

  does not contain a CMakeLists.txt file.


CMake Error at opt/CMakeLists.txt:90 (set_property):
  set_property could not find TARGET gtest.  Perhaps it has not yet been
  created.


CMake Error at opt/CMakeLists.txt:91 (set_property):
  set_property could not find TARGET gtest_main.  Perhaps it has not yet been
  created.


Set GTEST_FOUND to TRUE.
Set GTEST_LIBRARIES to gtest.
Set GTEST_MAIN_LIBRARIES to gtest_main.
Set GTEST_BOTH_LIBRARIES to gtest;gtest_main.
Set GTEST_INCLUDE_DIRS to /include.
Building mongoose project for test support.
CMake Error at opt/CMakeLists.txt:106 (add_subdirectory):
  The source directory

    D:/Developpement/C++/cpr-master/opt/mongoose

  does not contain a CMakeLists.txt file.


Set MONGOOSE_FOUND to TRUE.
Set MONGOOSE_LIBRARIES to mongoose.
Set MONGOOSE_INCLUDE_DIRS to .
CMake Error at opt/CMakeLists.txt:116 (set_property):
  set_property could not find TARGET mongoose.  Perhaps it has not yet been
  created.


Using CURL_INCLUDE_DIRS: /include;/include/curl.
Using CURL_LIBRARIES: libcurl.
Configuring incomplete, errors occurred!
See also "D:/Developpement/C++/cpr-master/build/CMakeFiles/CMakeOutput.log".
See also "D:/Developpement/C++/cpr-master/build/CMakeFiles/CMakeError.log".

我也尝试过使用gcc,但是他们没有说我们必须包含哪些文件,并且它返回仍然找不到标题。 有人可以描述复制CPR的步骤吗?还是发送HTTP请求的另一种简单方法?谢谢

2 个答案:

答案 0 :(得分:1)

使用git clone --recursive https://github.com/whoshuu/cpr/代替git clone https://github.com/whoshuu/cpr/

答案 1 :(得分:0)

在github here上有文档。

  

为使该库正常运行,我强烈建议   分叉示例项目。配置了最小的CMake   开始玩网络游戏所需的魔力和样板   应用程序。

     

如果您已经有一个项目,则需要将C ++请求与之集成在一起,   主要方法是使用git子模块。将此存储库添加为   您的根存储库的子模块:

     

git子模块添加git@github.com:whoshuu / cpr.git或git子模块添加   https://github.com/whoshuu/cpr.git

     

git子模块更新--init --recursive

     

接下来,在声明之前,将此子目录添加到您的CMakeLists.txt中。   任何可能使用它的目标:

     

add_subdirectory(cpr)

     

这将产生两个重要的CMake变量CPR_INCLUDE_DIRS和   CPR_LIBRARIES,您将以典型方式使用它:

     

include_directories($ {CPR_INCLUDE_DIRS})   target_link_libraries(您的target_name $ {CPR_LIBRARIES})

     

那应该做到的!使用集成C ++的子模块方法   请求,不需要自己处理libcurl,所有这些   依赖关系会为您解决。

(附带说明:您应该检查诸如conan或vcpkg之类的c ++程序包管理器,以查看是否捆绑在其中),可能更容易设置