Configuring libcurl in Visual Studio 2022

Configuring libcurl in Visual Studio 2022

Libcurl is freely available client side URL transfer library that is used to perform HTTP/HTTPS requests, FTP uploading and much more! click here. This blog comprises of steps to build libcurl, creating and configuring your first C++ project in Visual Studio.

SYSTEM REQUIREMENTS

  1. Windows 10/11 PC.
  2. Visual Studio 2022 Community/Professional Edition.
  3. x64 Native Tools Command Prompt for Visual studio or x86 as per the requirement.

BUILDING LIBCURL

Step 1) Download the curl zip folder from click here and extract it to a temporary folder in your desired location.

Step 2) Open the x64 Native Tools Command Prompt for Visual studio from start menu and change directory to winbuild folder location inside the curl directory.

image.png

Build curl from source using following commands for Debug and Release respectively.

nmake /f Makefile.vc mode=static vc=15 debug=yes
nmake /f Makefile.vc mode=static vc=15 debug=no

image.png

Step 3) Once both the commands are executed, the following directories will be created in $(curl_dir)/builds folder. In this case it will be created inside D:\Visual Studio\Projects\curl-7.83.1\curl-7.83.1\builds directory.

image.png

Step 4) The lib and includes directory can be located in "libcurl-vc15-x64-debug-static-ipv6-sspi-schannel" folder.

CREATING AND CONFIGURING C++ PROJECT IN VISUAL STUDIO

Step 1) Download and install Visual Studio 2022 from click here page if it is not already present on your machine.

Step 2) Create a New -> Empty Project and specify your project’s name, desired location.

Step 3) In the Solution Explorer right click on your project’s name and open the properties window.

Step 4) Select configuration as “All Configurations” and add ${curl-dir}\builds\libcurl-vc15-x64-debug-static-ipv6-sspi-schannel\include to Configuration Properties -> VC++ Directories -> Include Directories.

image.png

Step 5) Similarly, add add ${curl-dir}\builds\libcurl-vc15-x64-debug-static-ipv6-sspi-schannel\lib to Configuration Properties -> VC++ Directories -> Library Directories.

image.png

Step 6) Under Configuration Properties -> Linker -> System ensure that subsystem Is set as Console (/SUBSYSTEM:CONSOLE).

image.png

Step 7) Under Configuration Properties -> Linker -> Input, add the path to libcurl_a.lib and libcurl_a_debug.lib in the “Additional Dependencies” field.

image.png

Step 8) You can tell the linker to ignore the incorrect run-time libraries by using /NODEFAULTLIB for each library you want to ignore. In the Visual Studio IDE, separate the libraries to ignore by semi-colons in the Ignore Specific Default Libraries property.

image.png

SAMPLE C++ PROGRAM

Below is an example to perform HTTP GET request using libcurl. The expected output is a JSON response containing user details.

#include <iostream>
#include <string>
#define CURL_STATICLIB
#include "curl/curl.h"

#ifdef _DEBUG
#    pragma comment (lib,"curl/libcurl_a_debug.lib")
#else
#    pragma comment (lib,"curl/libcurl_a.lib")
#endif // _DEBUG

/*Windows Specific Additional Depenedencies*/
#pragma comment (lib,"Normaliz.lib")
#pragma comment (lib,"Ws2_32.lib")
#pragma comment (lib,"Wldap32.lib")
#pragma comment (lib,"Crypt32.lib")


static int writer(char* data, size_t size, size_t nmemb, std::string* writerData)
{
    if (writerData == NULL)
        return 0;

    writerData->append(data, size * nmemb);

    return size * nmemb;
}

int main(int argc, wchar_t* argv[]) {

    std::cout << "Hello curl";

    std::string content;

    curl_global_init(CURL_GLOBAL_ALL);
    CURL* curl = nullptr;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://reqres.in/api/users?page=2");
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);

        CURLcode code = curl_easy_perform(curl);

        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    std::cout << content;
    return 0;
}

OUTPUT

On successful compilation and execution we receive a JSON response as expected:

image.png

Hope this blog helps you to get started with your first C++ project that uses libcurl. Happy Learning!!

-Juhilee Nazare