curl/curl.h library not found in dev c++ 6.3 - c++

`#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}`
i have linked libcurl.a file in the project and added inlude folder from the curl-7.87.0_1-win64-mingw folder, i have also copied the include in C:\Program Files (x86)\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32 folder and copied the libraries too (as suggested in a previous answer to a similar question here) but still it isnt working, the error im seeing is [Error] curl/curl.h: No such file or directory.
this is the code im trying to run

Related

Unable to compile c++ code with Curl library, but Curl still works in the command line Windows 10

I recently installed curl on my Windows 10 computer via vcpkg. When I run it from the command line, everything seems to work fine. However, when I try and run this sample code on VS Code, I get a compiler error:
#include "curl/curl.h"
void testCurl() {
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
#ifdef SKIP_PEER_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
int main(){
testCurl();
return 0;
}
I am using powershell to compile. I've tried compiling with the following commands:
g++ CurlExample.cpp
g++ -lcurl CurlExample.cpp
g++ -lcurl/curl CurlExample.cpp
And I receive the following error:
CurlExample.cpp:1:10: fatal error: curl: No such file or directory
It seems as though VS Code recognizes the library, which it previously didn't before installing curl, so I am guessing I am missing a step with my compiler. Any suggestions?

Downloading a file via curl in c++ from dropbox

I want to download a file from a dropbox shared link using curl in a c++ program
I found a dropbox api pdf that showed me how to do it
#include <stdio.h>
#include <curl/curl.h>
int main (int argc, char *argv[])
{
CURL *curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
printf ("Running curl test.\n");
struct curl_slist *headers=NULL; /* init to NULL is important */
headers = curl_slist_append(headers, "Authorization: Bearer
<ACCESS_TOKEN>");
headers = curl_slist_append(headers, "Content-Type:");
headers = curl_slist_append(headers, "Dropbox-API-Arg:
{\"path\":\"/test.txt\"}");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL,
"https://content.dropboxapi.com/2/files/download");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
printf ("\nFinished curl test.\n");
}
curl_global_cleanup();
printf ("Done!\n");
return 0;
}
However, the comments supplied don't offer much explanation for me, and I can't get it to work.
I don't understand these three lines of code:
headers = curl_slist_append(headers, "Authorization: Bearer <ACCESS_TOKEN>");
headers = curl_slist_append(headers, "Content-Type:");
headers = curl_slist_append(headers, "Dropbox-API-Arg:{\"path\":\"/test.txt\"}");
I think I have to replace some stuff but I don't know what
"I think I have to replace some stuff but I don't know what" : Replace <ACCESS_TOKEN> with your actual access token.
You should also set the "Content-Type:" header to an appropriate value for the data you are fetching.
You must also change the value of the "Dropbox-API-Arg" header to match the file you are trying to get.
I finally found the solution to my problem.
Turns out I didn't have to use the Dropbox API
Here is the code
#include <iostream>
#include <curl/curl.h>
using namespace std;
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main(int argc, char** argv) {
CURL *curl;
FILE *fp;
const char* destination = "D:\\Desktop\\test.exe";
fp = fopen(destination, "wb");
curl = curl_easy_init();
/* A long parameter set to 1 tells the library to follow any Location: header
* that the server sends as part of an HTTP header in a 3xx response. The
*Location: header can specify a relative or an absolute URL to follow.
*/
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_URL, "https://www.dropbox.com/s/09nd26tdyto23yz/BankAccount.exe?dl=1"); // "dl=0"changed to "dl=1" to force download
// disabe the SSL peer certificate verification allowing the program to download the file from dropbox shared link
// in case it is not used it displays an error message stating "SSL peer certificate or SSH remote key was not OK"
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
CURLcode res;
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
if (res ==CURLE_OK)
cout << "OK";
else
cout << curl_easy_strerror(res);
return 0;
}
Thanks you guys for trying to help me. I appreciate

Getting cURL certificates to work with C++ and vcpkg install

so I have a class that makes an http call using curl in visual studios 2017 that was installed via vcpkg discussed: here, using curl_easy function calls:
string returnResponseAsString(string requestURL) {
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = (char *)malloc(1);
chunk.size = 0;
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/*Turn off SSL Verifcation*/
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, requestURL.c_str());
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent*/
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
printf("%lu bytes retrieved\n", (long)chunk.size);
}
string response = chunk.memory;
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return response;
}
If I dont include this line curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE); that turns off SSL verification, I get the error: curl_easy_perform() failed: Peer certificate cannot be authenticated with given CA certificates. I tried to install the certificates as per these instructions: link, but is says to place the downloaded certs in the same folder as the curl.exe. As far as I can tell there is no curl.exe installed by the vcpkg. I looked for .crt and I have an image of the found certs under vcpkg. Where Should I place the .crt file for authentication to work with visual studios?

Make libcurl in Borland C++ isn't successful

I want to compile libcurl with Borland C++. I make libcurl.lib by following code:
make borland
The lib file has made successfully. Now i want to build a test example. Something like this:
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
It compiles successfully. (The obj file has made.)
But it couldn't build. The following errors reported:
Error: Unresolved external 'InitializeCriticalSectionEx' referenced from C:\USERS\4L1R3Z4\DOWNLOADS\COMPRESSED\CURL\CURL\LIB\LIBCURL.LIB|asyn_thread
Error: Unresolved external 'GetTickCount64' referenced from C:\USERS\4L1R3Z4\DOWNLOADS\COMPRESSED\CURL\CURL\LIB\LIBCURL.LIB|timeval
Error: Unresolved external '__fstat' referenced from C:\USERS\4L1R3Z4\DOWNLOADS\COMPRESSED\CURL\CURL\LIB\LIBCURL.LIB|file
All libraries has added to Build configuration.
My OS is : Windows 7 64Bit.
I really don't know what library should i add to fix this issue!
Sincerely yours.

C++ cURL Refresh Page

I have the following code in C++
#include <cstdlib>
#include <iostream>
#include <curl/curl.h>
#include <string.h>
using namespace std;
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/mypage.html");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
system("PAUSE");
return EXIT_SUCCESS;
}
When I'm running this program it is showing the source code of mypage.html on the console. Next I updated the source code of mypage.html and again executed the program, but it was printing the previous source code on the console again. Where's the problem? Please Help.
I faced the same problem recently. But it was with WinINet, not with cURL. Simply I changed my .html file to .php on my server and it worked fine! Actually .php files aren't cached by browser. So give it a try.
I think you rewrite source code of the page, but remember update page on web server or page cashed