Using the curl library from C++ on Windows

curl is a project containing a command line tool and a library that can be used to transfer data using a variety of protocols, including, of course, HTTP and HTTPS. The library API is written in C, but there are various C++ wrappers on top of it. One of those is curlcpp. In this article, I will show how to build these libraries for Windows with Visual Studio.

Here is an example, using curlcpp, of how to get weather data from https://openweathermap.org.

Here is how you get this working on Windows using Visual Studio 2017. The following instructions are for 32-bit version, but you can do the same for 64-bit.

For libcurl:

  • Download CURL from https://curl.haxx.se/download.html.
  • Unzip and open the solution projects\Windows\VC15\curl-all.sln.
  • Build configurations LIB Release - DLL Windows SSPI and LIB Debug - DLL Windows SSPI for the target platform that you need (Win32 or x64)
  • Copy the output to build\lib\x86\ (or build\lib\x64\). To have both release and debug builds in the same folder name the debug one libcurld.lib.

For curlcpp:

  • Clone or download CURLCPP from https://github.com/JosephP91/curlcpp.
  • Create a subfolder called build in the project’s main folder.
  • Execute CMake from the build folder to create a Visual Studio solution. Here is an example that assums curl is in the same folder as curlcpp.
  • Open the generated project and build it.
  • Copy the output to lib\x86 (where lib is a subfolder in the curlcpp project main folder). To be able to have both Debug and Release builds in the same folder rename the Debug build to curlcppd.lib.

For your project using libcurl and curlcpp:

  • Add CURL_STATICLIB to the preprocessor definitions.
  • Add curl\include and curlcpp\include to the list of Additional Include Directories. (Make sure you include the correct relative paths.)
  • Add curl and curlcpp output folders, curl\build\lib\x86 and curlcpp\lib\x86\, to the Additional Library Directories.
  • Add the following static libraries to the list of Additional dependencies: libcurld.lib;curlcppd.lib;Crypt32.lib;ws2_32.lib;winmm.lib;wldap32.lib;

Attached is a demo project with libcurl and curlcpp builds for both 32 and 64-bit platforms.

Attachments

  • zip curldemo
    File size: 6 MB Downloads: 169

9 Comments on "Using the curl library from C++ on Windows"


  1. Salut Marius, ce se intampla daca una din astea 2 arunca exceptie? Mersi

    curl::curl_ios writer(str);

    curl::curl_easy easy(writer);

    easy.add(url.data());
    easy.add(1L);


  2. Hellos,

    Thanks for the tutorial. I followed all the steps , but still i face linker issue as below
    Can you help me in knowing the reason,it has been 5 hours trying to resolve that

    error LNK2019: unresolved external symbol _curl_global_init referenced
    error LNK2019: unresolved external symbol _curl_easy_init referenced in function


  3. @Tano, it’s shorted. As long as you know (and you should know) that “”s is a standard user-defined literal for creating string objects, why bother writing std::string? Especially if you prefer to use auto whenever possible, like I do, in which case you are being consistent with other object declarations.


  4. @Marius
    I was thinking that when reaading just the first part of the line “std::string” you know what the variable type is.
    But with “auto” (which I like but not all the time), you have to read until the last character (to the last ‘s’) to determine what’s the type for appid.
    If the line is 100 character long, it’s a bit strange to have to read until the end of line to determine the type…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.