Make libcurl in Borland C++ isn't successful - c++

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.

Related

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

`#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

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?

Using libcurl from C++/Windows to post to an API that requires a client certificate for authentication? Getting CURLE_SSL_CERTPROBLEM

I have searched here, but did not find anything that matched. What I have tested with curl.exe and that works fine. I then tried a small test program. Created a commandline Win32 project in VS2017 and copied in the code from simplessl.c. Problem is that no matter what certificates I try I always get CURLE_SSL_CERTPROBLEM. This points to the client certificate being wrong. I even tried with the 2048 bit certificate and key from here: https://fm4dd.com/openssl/certexamples.shtm Not sure how to troubleshoot.
The test program I used:
#include "pch.h"
#include <iostream>
#include <curl/curl.h>
int main()
{
std::cout << "Hello World!\n";
CURL *curl;
CURLcode res;
FILE *headerfile;
const char *pPassphrase = NULL;
static const char *pCertFile = "C:\\Prog\\TestCurl\\2048b-rsa-example-cert.pem";
static const char *pCACertFile = "cacert.pem";
static const char *pHeaderFile = "C:\\Prog\\TestCurl\\requestWith.txt";
const char *pKeyName;
const char *pKeyType;
const char *pEngine;
#ifdef USE_ENGINE
pKeyName = "rsa_test";
pKeyType = "ENG";
pEngine = "chil"; /* for nChiper HSM... */
#else
pKeyName = "C:\\Prog\\TestCurl\\2048b-rsa-example-keypair.pem";
pKeyType = "PEM";
pEngine = NULL;
#endif
headerfile = fopen(pHeaderFile, "wb");
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
/* what call to write: */
//curl_easy_setopt(curl, CURLOPT_URL, "https://pintatesti.vero.fi/FIS/Return/IIT/Test/GetWithholdingPercentage/v1");
curl_easy_setopt(curl, CURLOPT_URL, "https://www.pedago.fi");
curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);
do { /* dummy loop, just to break out from */
if (pEngine) {
/* use crypto engine */
if (curl_easy_setopt(curl, CURLOPT_SSLENGINE, pEngine) != CURLE_OK) {
/* load the crypto engine */
fprintf(stderr, "can't set crypto engine\n");
break;
}
if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK) {
/* set the crypto engine as default */
/* only needed for the first time you load
a engine in a curl object... */
fprintf(stderr, "can't set crypto engine as default\n");
break;
}
}
/* cert is stored PEM coded in file... */
/* since PEM is default, we needn't set it for PEM */
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
/* set the cert for client authentication */
curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);
/* sorry, for engine we must set the passphrase
(if the key has one...) */
if (pPassphrase)
curl_easy_setopt(curl, CURLOPT_KEYPASSWD, pPassphrase);
/* if we use a key stored in a crypto engine,
we must set the key type to "ENG" */
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, pKeyType);
/* set the private key (file or ID in engine) */
curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyName);
/* set the file with the certs vaildating the server */
//curl_easy_setopt(curl, CURLOPT_CAINFO, pCACertFile);
/* disconnect if we can't validate server's cert */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
/* 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));
/* we are done... */
} while (0);
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
Edit: Underlying error I get is " schannel: certificate format compatibility error" and only occurrence I can find is in schannel.c where it compares to a P12 type, but my certs are PEM?
Edit2: So it does work if I import the certificate into the local store, something I would definitely want to avoid! Also curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); appears to be avery good idea! Now getting further.
Edit3: Got it working. Possibly related to: Having trouble sending client certificate in libcurl ssl request, what am I missing? which is from 2019. I would very much prefer to have the certificates in a file though.
Edit4: "libcurl/7.79.1 Schannel WinIDN"
I solved this with a little help from my friends. Libcurl when compiled as default on Windows will default to schannel. To override that you need to build for OpenSSL, thusly: nmake /f Makefile.vc mode=static DEBUG=yes WITH_SSL=static WITH_DEVEL=C:\Prog\OpenSSL-Win32_111L\. The important keywords are WITH_SSL=static or dynamic and WITH_DEVEL= path to where your OpenSSL files live. When built this way you can give file paths for certificate and key, ie: curl_easy_setopt(curl, CURLOPT_SSLCERT, m_Certificate.GetString());
This frees you of the messy certificate handling in Windows and lets you keep the certificates with the app itself.

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?

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