How To Set-Up libcurl on Visual Studio 2019
Even though a Windows lover, I should say that working with C++ projects in the Windows environment is not always a pleasant experience. This is mostly due to the challenge of setting up the environment. And sometimes, it is fairly a daunting task. Once everything is set-up, everything becomes smooth and life becomes a lot beautiful. But up until that point, it is one of the worst nightmares driving programmers crazy.
Recently, I tried to work with the libcurl library on my Windows machine and with no surprise, got hit with numerous errors mostly from the linker.
After investing an appropriate amount of time, I got it to work. So I thought it would be helpful for others to share the steps I followed to get the libcurl to work on my Windows machine without any of that payment in time.
Launching Visual Studio Developer Powershell
The build process requires the Visual Studio Developer Command Prompt or Developer Powershell.
They can be found under Tools -> Command Line on the Visual Studio 2019 Menubar.
I used Powershell and it worked fine. Command Prompt should also work.
Building libcurl from Source
First, download the latest source from the Github.
PS ${curl-dir}> git clone https://github.com/curl/curl.git
To generate the tool_hugehelp.c file, run the following command.
PS ${curl-dir}> buildconf.bat
Otherwise, during the build, it will fail with the message -
NMAKE: fatal error U1073: don’t know how to make ‘..\src\tool_hugehelp.c’
Then, from the winbuild directory, start the build with the following commands.
PS ${curl-dir}> cd winbuildPS ${curl-dir}\winbuild> nmake /f Makefile.vc mode=static
The build will start. It will take several minutes to finish. Upon finishing, ${curl-dir}\builds will contain the build files.
Setting-up Project Configuration
1. Create a new /Open an existing C++ project.
2. Open Project Properties.
3. Add ${curl-dir}\builds\libcurl-vc-x86-release-static-ipv6-sspi-winssl\include to Configuration Properties -> VC++ Directories -> Include Directories.
4. Add ${curl-dir}\builds\libcurl-vc-x86-release-static-ipv6-sspi-winssl\lib to Configuration Properties -> VC++ Directories -> Library Directories.
5. Add CURL_STATICLIB to Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions.
6. In Configuration Properties -> Linker -> Input -> Additional Dependencies, add these followings lines-
- ${curl-dir}\builds\libcurl-vc-x86-release-static-ipv6-sspi-winssl\lib\libcurl_a.lib
- Ws2_32.lib
- Wldap32.lib
- Crypt32.lib
- Normaliz.lib
Check If Everything is Set-up Correctly
Now you can try if everything is working with the following code-
#include <iostream>
#include "curl/curl.h"using namespace std;int main(int argc, char *argv[])
{
CURL *req = curl_easy_init();
CURLcode res; if (req)
{
curl_easy_setopt(req, CURLOPT_URL, "www.google.com");
curl_easy_setopt(req, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(req); if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_operation() failed : %s\n", curl_easy_strerror(res));
}
} curl_easy_cleanup(req); return 0;
}