Using Libcurl in C++ program

Hi! Hope you're having a good day. I'm very new to C++ programming - and programming in general - and I wanted to ask a quick question!

I am currently working on a project that uses a web API (IEX) to get stock market data. In order to get the data, I am trying to download and use libcurl in my C++ program. However, unlike python where I can just use pip to download a third-party library, I guess you have to download and build libraries for C++ yourself? I am currently running Windows 10 64 bit and I use mingw to compile my programs on the command line. So I have some questions:

1.) There are a million download options on the curl website. Should I download the binary or should I download the source code and build it?

2.) The steps I have taken so far: I downloaded the zip file under "Source Archivess" at https://curl.haxx.se/download.html. I then unzipped it where I wanted it. Then I went to the root directory, and I used the command "mingw32-make mingw32". This compiled. I then compiled my C++ program using "g++ -Ipath/to/include -Ipath/to/lib -o program.cpp program -lcurl", but I get the error:undefined reference to `_imp__curl_easy_cleanup'. This means that the library isn't getting linked, right? There is a libcurl.dll and libcurl.a in path/to/curl/lib - am I supposed to move that to the directory with my working program? When I move the .dll file to my working directory and use -lcurldll instead of -lcurl, it compiles, but when I try to run my executable this pops up: Error from cURL: Unsupported protocol.


3.) From what i've read online, but it could be anything, the above errors are because I didn't compile curl using -ssl? So when I restart, download it again, and try to compile with the command "mingw32-make mingw32-ssl", I get the error: "Invalid path to OpenSSL package: ../../openssl-1.0.2a. Stop." Clearly the makefile couldn't find the OpenSSL. Am i supposed to download OpenSSL and move it into a specific directory? Would I have to build that too?

Clearly, I have no idea what i'm doing. But I have been going after this for 3 days straight and could use any advice yall have!

EDIT: I have literally read every scrap of information on the internet about this lol. I also found a version (im guessing its a binary download?) that has a libcurl.dll.a and a libcurl.a and a include folder, how do I use these things to link to my program if the above wasn't correct?

I apologize for such a long post and I will of course be your slave for helping me :(

-color
Last edited on
> g++ -Ipath/to/include -Ipath/to/lib -o program.cpp program -lcurl"
-I is for the compiler, to find .h files.
-L is for the linker, to find libraries.

Be careful what you put next to the -o flag, because that's going to get overwritten by the generated executable.

So perhaps
g++ -Ipath/to/include -Lpath/to/lib -o program program.cpp -lcurl

As for downloads, https://curl.haxx.se/windows/ should be all you need.
Just download the package for your OS (32 or 64 bit) and unzip the file somewhere.
Then just point your /path/to's to the unzipped include and lib directories.

Salem,

You're a hero for always helping me, so thank you! I did as you said, but unfortunately I am still getting the error:

".../Temp\ccMoLMZ1.o:ds.cpp:(.text+0x1d): undefined reference to `_imp__curl_easy_init'"

There is a libcurl.dll.a and libcurl.a file in the path that I used with -L"path...", so i'm not sure why it still isn't linking :(

Currently compiling as "g++ -I"C:/curl-7.64.0-win64-mingw/include/" -L"C:/curl-7.64.0-win64-mingw/lib/" -o end ds.cpp -lcurl" and getting the above error.

EDIT: The -I"path to include" seems to be working, because without it I am getting "undefined reference to curl/curl.h", so it must be picking it up. So the only problem is with linking it

EDIT 2: I downloaded the 32 bit version of libcurl and I was able to successfully compile my program, but when I try to run it this pops up: "The code execution cannot proceed because libssl-1-1.dll was not found..".

EDIT3: I downloaded openSSL for windows32 and windows64 (lol didn't know which I needed), and when I run the program I am now getting the error: curl_easy_perform() failed: SSL peer certificate or SSH remote key was not OK. Currently trying to disable it because I like to live life on the edge.

EDIT4: after looking into it, disabling it might not be the best idea because your connection will be vulnerable to a mitm attack. So currently looking at downloading certs from the curl website...https://curl.haxx.se/docs/caextract.html

EDIT 5: BOYS WE ARE IN!!!!!!!!! WE HACKED THE MAINFRAME. AFTER 4 DAYS I FINALLY GOT LIBCURL WORKING. Big shoutout to SALEM C, the hero cplusplus forums deserves, and also the one it needs.

To all those who are having similar trouble:
1.)Check if you're compiling with mingw32 or mingw64. If you're using mingw32 like me, then you'll need to download the 32 bit version for libcurl (you're downloading the binary and making a 32 bit program because mingw32 makes 32 bit programs i believe). Otherwise download the 64 bit.
2.) I am compiling using "g++ -o end ds.cpp -IC:\curl-7.64.0-win32-mingw\include -LC:\curl-7.64.0-win32-mingw\lib -lcurl"
3.) Finally, in your download of libcurl, go to bin and move the file "libcurl.dll" into your working directory (the one with your executable/program/compiling in). Hopefully it now works
4.) If you are getting something like "The code execution cannot procceed because libssl-1_1.dll was not found", then you'll need to download opensll from https://slproweb.com/products/Win32OpenSSL.html
5.) Your program should now be working. If you are getting this error: SSL peer certificate or SSH remote key was not OK. Include these 2 lines in your program (it worked for me, but also I don't even know how to use libcurl yet):
-curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
-curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

Hopefully your program is now working
:)



-color
Last edited on
Sorry to comment on an old thread but seeing as it wasn't mentioned have you not thought about trying this...

C++ Requests: Curl for People
https://youtu.be/f_D-wD1EmWk
https://github.com/whoshuu/cpr
Last edited on
Topic archived. No new replies allowed.