使cURL与Visual Studios 2017一起使用 [英] Getting cURL to work with Visual Studios 2017

查看:103
本文介绍了使cURL与Visual Studios 2017一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*按照以下步骤,我使CURL在64位计算机上的VS 2017中工作(请参阅下面的原始问题):

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

首先安装vcpkg:

  1. 使用gitbash将 vcpkg 克隆到C:\Program Files
  2. 在命令提示符下,导航到C:\Program Files\vcpkg
  3. 在命令提示符下运行:.\bootstrap-vcpkg.bat
  4. 在命令提示符下运行:vcpkg integrate install
  1. Clone vcpkg using gitbash into C:\Program Files
  2. In a command prompt navigate to C:\Program Files\vcpkg
  3. Run in the command prompt: .\bootstrap-vcpkg.bat
  4. Run in the command prompt: vcpkg integrate install

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

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

  1. 打开VS 2017 Command prompt并导航到vcpkg文件夹(vcpkg.exe所在的位置)
  2. 运行:vcpkg install curl[*]:x64-windows(请注意,下载和运行此过程可能需要大约半小时,请放心,如果它看起来卡在"了部件上).

  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:以前,我的指令说要运行vcpkg install curl:x64-windows,但我是根据@ i7clock的要求在[*]上添加的,以启用sftp和scp协议.

* 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.

在此步骤之后,您应检查并确保正确安装了curl.为此,您应该在VS 2017中创建一个新项目,并尝试包含#include curl/curl.h而不添加任何其他包含目录.如果您不能执行此操作,则说明curl的安装有问题.您应该删除curl(甚至可能是vcpkg文件夹并进行全新安装),直到可以包含curl/curl.h.

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.

*重要说明:仅当您在x64中使用x64调试器/编译时,这才起作用!如果您不能包括curl目录,请检查以确保将调试设置为Windows的正确版本.

*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.

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

You may need to disable SSL peer verification as well:

  1. 将代码curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);放在请求之前(请参见下文).请注意,这仅是必要的,因为我无法弄清楚如何使证书与curl配合使用.我有关于此问题的尚未答复的stackoverflow帖子
  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. 导航到vcpkg \ packages \ curl_x64-windows \ lib以找到libcurl.lib文件.
  2. 在属性"->链接器"下的其他库目录"中包含libcurl.lib的路径
  3. 链接器->输入->其他依赖项下的其他依赖项中包含libcurl.lib
  4. 在属性-> C/C ++->预处理器->预处理器定义中放置CURL_STATICLIB
  1. Navigate to vcpkg\packages\curl_x64-windows\lib 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\n",
       curl_easy_strerror(res));

       curl_easy_cleanup(curl);
 }
 curl_global_cleanup();
}

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

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

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

我正在尝试使用cURL进行API调用,这样我就可以开始获取实时库存数据,但是要使其在VS 2017中运行却遇到了困难.我尝试了

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:

根据vcpkg文档,我现在应该可以简单地#include,但是找不到该文件夹​​.如果尝试从vcpkg \ packages \ curl_x86 \ include和#include中包含"include"目录,则可以构建我的项目.我也可以访问某些类,但是如果尝试像本例中那样设置curl_global_init(CURL_GLOBAL_DEFAULT),则会出现链接器错误.

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 vcpkg\packages\curl_x86\include 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.

推荐答案

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

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

>vcpkg install curl:x64-windows

这篇关于使cURL与Visual Studios 2017一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆