Getting cURL to work with Visual Studios 2017

14,083

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

Share:
14,083

Related videos on Youtube

EliSquared
Author by

EliSquared

Updated on June 04, 2022

Comments

  • EliSquared
    EliSquared over 1 year

    *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 Files\vcpkg
    3. Run in the command prompt: .\bootstrap-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 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;
    }
    

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

    Linker error curl_global_init error

    • Asesh
      Asesh almost 6 years
      What version of Visual C++ was that cURL library compiled against? It's probably because of version mismatch
    • EliSquared
      EliSquared almost 6 years
      How could I figure that out? Though I thought if I used vcpkg, it would automatically pick up the correct cURL library.
    • Asesh
      Asesh almost 6 years
      Use dumpbin, it's bundled with VS: dumpbin /rawdata:1 "c:\my_curl_lib.lib". After executing that command, look at the value of _MSC_VER from RAW DATA #1
    • EliSquared
      EliSquared almost 6 years
      hmm, when I tried that dumpbin command all I get it a stream of text on my command prompt, followed by a summary of .data, .rdata., .reloc, .rsrc and .text. Not sure where the _MSC_VER is supposed to be.
    • EliSquared
      EliSquared almost 6 years
      ok so I figured out how to output to a file and ran dumpbin on libcurl.lib. Raw data #1 does not have an _MSC_VER on it.
    • Asesh
      Asesh almost 6 years
      But I see it here: RAW DATA #1 00000000: 20 20 20 2F 46 41 49 4C 49 46 4D 49 53 4D 41 54 /FAILIFMISMAT 00000010: 43 48 3A 22 5F 4D 53 43 5F 56 45 52 3D 31 38 30 CH:"_MSC_VER=1800
    • EliSquared
      EliSquared almost 6 years
      Ya, my output is: RAW DATA #1 00000000: 02 00 00 00 12 00 09 00 00 00 00 00 0B 6C 69 62 .............lib 00000010: 63 75 72 6C 2E 64 6C 6C 27 00 13 10 07 00 00 00 curl.dll'....... 00000020: 03 00 00 00 00 00 00 00 0E 00 0B 00 A4 63 12 4D ............¤c.M 00000030: 69 63 72 6F 73 6F 66 74 20 28 52 29 20 4C 49 4E icrosoft (R) LIN 00000040: 4B K
  • EliSquared
    EliSquared almost 6 years
    Yes this worked! I can now compile all of the commented out code. I'll update my instruction. Now the problem is I am getting is a "Cannot authenticate certificate error", but I overrode it with the line curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);.
  • rustyx
    rustyx almost 6 years
    Setting verifypeer to false means you trust any certificate, even self-signed. Which reduces security essentially to 0.
  • EliSquared
    EliSquared almost 6 years
    Would you have any suggestions on how to fix @RustyX? I made a stack overflow post for this question here: stackoverflow.com/questions/47963081/….
  • krjw
    krjw almost 5 years
    I got this to work thanks! Do you know how to use sftp or scp? I cant get them to run... I always get "unsupported or disabled protocol"
  • krjw
    krjw almost 5 years
    Ok I found out why this does not work... could you edit your answer? vcpkg install curl[*]:x64-windows for all protocalls
  • B. Hoeper
    B. Hoeper almost 4 years
    What should I do if I need both x86 and x64 build versions?