使 cURL 与 Visual Studios 2017 一起使用

2021-12-17 00:00:00 curl visual-studio-2017 linker c++ vcpkg

*按照以下步骤,我在 VS 2017 上的 VS 2017 中按照以下步骤运行了 CURL(原始问题见下文):

首先安装 vcpkg:

  1. 使用 gitbash 将 vcpkg 克隆到 C:Program Files
  2. 在命令提示符中导航到 C:Program Filesvcpkg
  3. 在命令提示符下运行:.ootstrap-vcpkg.bat
  4. 在命令提示符下运行:vcpkg集成安装

然后使用 vcpkg 和 Visual Studios 2017 命令提示符安装 cURL:

  1. 打开 VS 2017 命令提示符 并导航到 vcpkg 文件夹(vcpkg.exe 所在的位置)
  2. 运行:vcpkg install curl[*]:x64-windows(注意这可能需要大约半小时来下载和运行,如果它看起来像卡在零件上).

    *以前我的指令说要运行 vcpkg install curl:x64-windows 但我在@i7clock 的要求下添加了 [*] 以启用 sftp和 scp 协议.

  3. 在这一步之后,您应该检查以确保 curl 安装正确.为此,您应该在 VS 2017 中创建一个新项目并尝试包含 #include curl/curl.h 而不添加任何其他包含目录.如果您不能这样做,那么您的 curl 安装就会出现问题.您应该删除 curl(甚至可能删除 vcpkg 文件夹并进行全新安装),直到您可以包含 curl/curl.h.

    *重要提示:这仅在您使用 x64 调试器/在 x64 中编译时有效!如果您无法包含 curl 目录,请检查以确保您的调试设置为正确的 Windows 版本.

您可能还需要禁用 SSL 对等验证:

  1. 将代码 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); 放在请求之前(见下文).请注意,这只是必要的,因为我无法弄清楚如何获取证书以使用 curl.我有一个关于这个问题的迄今为止尚未回答的 stackoverflow 帖子 这里.

这里有一些其他步骤,您可能需要尝试让事情运行,但我最终发现它们没有必要:

  1. 导航到 vcpkgpackagescurl_x64-windowslib 以找到 libcurl.lib 文件.
  2. 在属性 -> 链接器下的附加库目录中包含 libcurl.lib 的路径
  3. 在链接器下的附加依赖项中包含 libcurl.lib -> 输入 -> 附加依赖项
  4. CURL_STATICLIB 放在属性 -> C/C++ -> 预处理器 -> 预处理器定义中

这是我现在的工作代码:

#include "curl/curl.h"无效测试卷曲(){卷曲 *卷曲;CURLcode res;curl_global_init(CURL_GLOBAL_ALL);curl = curl_easy_init();如果(卷曲){curl_easy_setopt(卷曲,CURLOPT_SSL_VERIFYPEER,FALSE);curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");#ifdef SKIP_PEER_VERIFICATIONcurl_easy_setopt(卷曲,CURLOPT_SSL_VERIFYPEER,0L);#万一#ifdef SKIP_HOSTNAME_VERIFICATIONcurl_easy_setopt(卷曲,CURLOPT_SSL_VERIFYHOST,0L);#万一res = curl_easy_perform(curl);如果 (res != CURLE_OK)fprintf(stderr, "curl_easy_perform() 失败:%s
",curl_easy_strerror(res));curl_easy_cleanup(卷曲);}curl_global_cleanup();}int main(){测试卷曲();返回0;}

*这是我的旧问题在修复之前的其余解释:

我正在尝试使用 cURL 进行 API 调用,以便我可以开始获取实时股票数据,但我在 VS 2017 中遇到了困难.我尝试了

解决方案

您已经使用 vcpkg 安装了 x86 版本的 curl(即 vcpkgpackagescurl_x86include 中的 x86).您需要安装 x64 版本以匹配您的项目:

>vcpkg install curl:x64-windows

*Edit: I got CURL working in VS 2017 on a 64 bit machine following these steps (see below for original problem):

First install vcpkg:

  1. Clone vcpkg using gitbash into C:Program Files
  2. In a command prompt navigate to C:Program Filesvcpkg
  3. Run in the command prompt: .ootstrap-vcpkg.bat
  4. Run in the command prompt: vcpkg integrate install

Then use vcpkg and Visual Studios 2017 command prompt to install cURL:

  1. Open a VS 2017 Command prompt and navigate to the vcpkg folder (where the vcpkg.exe is)
  2. Run: vcpkg install curl[*]:x64-windows (note this can take around a half hour to download and run, don't worry if it looks like it is "stuck" at parts).

    *Edit: previously my instructions said to run vcpkg install curl:x64-windows but I added on the [*] at the behest of @i7clock to enable sftp and scp protocols.

  3. After this step, you should check to make sure that curl installed correctly. To do this you should create a new project in VS 2017 and try and include #include curl/curl.h without adding any additional include directories. If you cannot do this then something went wrong with your install of curl. You should remove curl (and perhaps even the vcpkg folder and do a clean install) until you can include curl/curl.h.

    *Important Note: this will only work if you are using x64 debugger/compiling in x64! If you cannot include the curl directory check to make sure your debug is set to the correct version of Windows.

You may need to disable SSL peer verification as well:

  1. Place the code curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); before the request (see below). Note this is only necessary because I could not figure how to get certificates to work with curl. I have an as-of-yet unanswered stackoverflow post regarding this problem here.

Here are some other steps you may need to try to get things running, but I ended up finding them not necessary:

  1. Navigate to vcpkgpackagescurl_x64-windowslib to find the libcurl.lib file.
  2. Include the path to libcurl.lib in Additional Library Directories under Properties -> Linker
  3. Included libcurl.lib in Additional Dependencies under Linker -> Input -> Additional Dependencies
  4. Place CURL_STATICLIB in Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions

Here is my now working code:

#include "curl/curl.h"


void testCurl() {
    CURL *curl;
    CURLcode res; 

    curl_global_init(CURL_GLOBAL_ALL); 

    curl = curl_easy_init();
    if (curl) {
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");

    #ifdef SKIP_PEER_VERIFICATION
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    #endif

    #ifdef SKIP_HOSTNAME_VERIFICATION
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
    #endif

    res = curl_easy_perform(curl);

   if (res != CURLE_OK)
       fprintf(stderr, "curl_easy_perform() failed: %s
",
       curl_easy_strerror(res));

       curl_easy_cleanup(curl);
 }
 curl_global_cleanup();
}

int main(){
    testCurl();
    return 0;
}

*Edit: Here is the rest of the explanation of my old problem before it was fixed:

I am trying to use cURL to make an API call so I can start getting real time stock data, but I am running into difficulties getting it to function in VS 2017. I have attempted an install using vcpckg using the following steps:

According to vcpkg documentation I should be able to now simply #include , but it can't find the folder. If I try including the "include" directory from vcpkgpackagescurl_x86include and #include I can build my project. I can also access some of the classes, but if I try and set the curl_global_init(CURL_GLOBAL_DEFAULT) as in this example I get linker errors.

解决方案

You've installed the x86 version of curl with vcpkg (That's the x86 in vcpkgpackagescurl_x86include). You need to install the x64 version to match your project:

>vcpkg install curl:x64-windows

相关文章