downloaded precompiled libcurl from github, linked library to project and included header file and i'm trying to run simple code
#include <iostream>
#include <string>
#include <curl/curl.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
}
return 0;
}
but i'm receiving errors
undefined reference to _imp__curl_easy_init
undefined reference to _imp__curl_easy_setopt
undefined reference to _imp__curl_easy_perform
undefined reference to _imp__curl_easy_cleanup
can anyone help me? i'm on windows 10 and using codeblocks 17
You should add this define before including curl:
#define CURL_STATICLIB
It seems like you're statically linking to curl, but curl requires to be told about this in order for it to expose the implementation.
Related
Trying to store the data out of the function and use it for another function from res variable, but still to no avail.
Been able to extract the data from the website and looks like this:
Approximate: 19879888
#include <stdio.h>
#include <iostream>
using namespace std;
#define CURL_STATICLIB
#include <curl/curl.h>
int main()
{
CURLcode res;
CURL* curl = curl_easy_init();
struct curl_slist* header = NULL;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "website for c++ practice");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return 0;
}
Been searching a lot already but cant find a direct answer. Help would be greatly appreciated!
Use the CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATA options to save the content to a variable, eg:
#include <iostream>
#include <string>
using namespace std;
#define CURL_STATICLIB
#include <curl/curl.h>
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
size_t total = size * nmemb;
static_cast<string*>(userdata)->append(ptr, total);
return total;
}
int main()
{
CURL* curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "website for c++ practice");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
string data;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
CURLcode res = curl_easy_perform(curl);
// use res and data as needed...
curl_easy_cleanup(curl);
}
return 0;
}
I have project with curl but size my file 720KB. This is a lot for me. I am use curl only for smtp, how i can reduce my file. I tried use defines discribed in CURL-DISABLE.md but that not worked, i think i configured them incorrectly. Can you give me instruction how i can reduce curl? My code of program.
#include <iostream>
#include <windows.h>
#define CURL_STATICLIB
#include "curl/curl.h"
#pragma comment(lib, "libcurl_a.lib")
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
}
return 0;
}
It work but a lot of size. SCREEN_OF_FILE_IN_PATH
I've got a short C++ program to download a file with curl.
I can compile it with:
gcc main.cpp -L/usr/include/x86_64-linux-gnu/curl -lcurl
but when I try it to compile for my arm-development board with the command:
arm-linux-gnueabihf-gcc main.cpp -I/usr/include/x86_64-linux-gnu -L/usr/include/x86_64-linux-gnu/curl -lcurl
I got an error, that lcurl cannot be found.
Can somebody tell me how to compile my program the right way or how to install curl for arm on my Linux-Ubuntu-Machine
#include <curl/curl.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main()
{
CURL *curl;
FILE *fp;
CURLcode res;
char *url = "...url...";
char outfilename[FILENAME_MAX] = "/home/test.txt";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
// always cleanup
curl_easy_cleanup(curl);
fclose(fp);
}
return( 0 );
}
I have installed the cygwin64 on windows 10, also there are installed the g++ and the libraries of libCurl. When I run the code the g++ compiler throw me this error '#error Only Win32 target is supported!'.
How I solve this error?
Have I to install another version or library?
TestCurl.cpp
#include <iostream>
#include <string>
#include <curl/curl.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
}
return 0;
}
MakeFile
CC=g++
MIN_GW = C:\cygwin64\usr\x86_64-w64-mingw32\sys-root\mingw\include
all: call.o
call.o: call.cpp
$(CC) call.cpp -o call -I$(MIN_GW)
Error
Errorlog
In my program, I want to download a few files. So I took cURL and used this code (taken and modified a little bit from here Download file using libcurl in C/C++):
#include "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 DlZip(){
CURL *curl;
FILE *fp;
CURLcode res;
string url = "http://wordpress.org/extend/plugins/about/readme.txt";
char outfilename[FILENAME_MAX] = "/Users/Me/Desktop/bbb.txt";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
But nothing happened and there weren't any file on my desktop :-/
What is the problem with my code?
Or if you have a simple function to use, could you give me ?
Thanks!
Check Below code
#include <cstdio>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>
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(){
CURL *curl;
FILE *fp;
CURLcode res;
string url = "http://www.joes-hardware.com/tools.html";
char outfilename[FILENAME_MAX] = "./MyText.txt";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
It works fine.
The problem is - this code is not able to use https and hence when the url provided by you when opened in browser and through above code - produce separate responses.