Last active
October 4, 2023 12:49
-
-
Save ericdennisforever/4127d7ec82fbb23f1fbf872f10e02359 to your computer and use it in GitHub Desktop.
Using the curl library from C++ on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
#include "curl_easy.h" | |
#include "curl_form.h" | |
#include "curl_ios.h" | |
#include "curl_exception.h" | |
std::stringstream get_response(std::string_view url) | |
{ | |
std::stringstream str; | |
try | |
{ | |
curl::curl_ios<std::stringstream> writer(str); | |
curl::curl_easy easy(writer); | |
easy.add<CURLOPT_URL>(url.data()); | |
easy.add<CURLOPT_FOLLOWLOCATION>(1L); | |
easy.perform(); | |
} | |
catch (curl::curl_easy_exception const & error) | |
{ | |
auto errors = error.get_traceback(); | |
error.print_traceback(); | |
} | |
return str; | |
} | |
int main() | |
{ | |
using namespace std::string_literals; | |
auto appid = "0c7978..."s; | |
auto location = "Timisoara"s; | |
auto url = "https://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + appid; | |
auto json = get_response(url); | |
std::cout << json.str() << std::endl; | |
return 0; | |
} | |
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. | |
cmake -G "Visual Studio 15 2017" .. -DCURL_LIBRARY=..\curl\build\lib\x86\libcurld.lib -DCURL_INCLUDE_DIR=..\curl\include | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment