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.
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);
Se intampla ca ai o exceptie neprinsa. Asa se intampla cand copiezi exemplele din documentatia proiectului (https://github.com/JosephP91/curlcpp) si nu-ti pui intrebari dinastea.
O sa modific codul pentru a evita asta. Multumesc.
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
Did you defined CURL_STATICLIB? And are your static lib files available in the right location? I have attached a demo. Does that work for you?
Salut Marius,
Ce avantaj are scrierea in locul ?
Mersi
auto appid = “0c7978…”s;
vs
std::string appid = “0c7978…”;
@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.
@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…
You should use auto when the time is not that important.
on BuildIt
Erro C1083 Não é possível abrir arquivo incluir: ‘curl/curl.h’: No such file or directory curlpp_static C:\Users\Desenvolvimento\Documents\Visual Studio 2017\Projetos\DemoAPP\ModeloAPi\lib\curlpp-0.8.1\include\curlpp\Form.hpp
The same happens if I try with “libcurl”, however the file is “stdio.h”
Can’t open curl-all.sln, getting this:
C:\Users\vlad\Source\Repos\curl-master\projects\Windows\VC15\src\curl.vcxproj : error : The project file could not be loaded. Could not find file ‘C:\Users\vlad\Source\Repos\curl-master\projects\Windows\VC15\src\curl.vcxproj’. C:\Users\vlad\Source\Repos\curl-master\projects\Windows\VC15\src\curl.vcxproj
C:\Users\vlad\Source\Repos\curl-master\projects\Windows\VC15\lib\libcurl.vcxproj : error : The project file could not be loaded. Could not find file ‘C:\Users\vlad\Source\Repos\curl-master\projects\Windows\VC15\lib\libcurl.vcxproj’. C:\Users\vlad\Source\Repos\curl-master\projects\Windows\VC15\lib\libcurl.vcxproj
These files are NOT in the git repo.
When opening the solution in VS 2019 I get this error:
$(SolutionDir)externals\curl\projects\Windows\VC15\lib\libcurl.vcxproj : error : Project “$(SolutionDir)externals\curl\projects\Windows\VC15\lib\libcurl.vcxproj” could not be found.
Thanks a lot,i was struggling a bit to get binary files out of curl cpp wrappers.The blog really helped to sort out issues in short span of time.
Thank you for the information. It’s really useful. I could use it without problems in a project made in console mode with Visual Studio, but I have problems when using with MFC libraries in a windows app. Do you know if is any incompatibility withh this kind of DLLs. Thanks
I have made void VC 2019 project and copy past your sample code in it. Then made all your additional steps. Remains one problem with these two lines
easy.add(url.data());
easy.add(1L);
saying: Identifier “CURLOPT_URL” is undefined and the same for CURLOPT_FOLLOWLOCATION.
What is missing in my project?
I don’t why, but these identifiers have been stripped in the two lines of my comment, but they are in the code.
Oliver, you just need to include <curl/curl.h> for these (they are actually defined in curl_easy.h). See the docs: https://curl.se/libcurl/c/CURLOPT_URL.html and https://curl.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html.