I'm very new to c++ I've downloaded and compiled a few examples without issue.
Now I'm trying to download 10 files from a server and write each one to a different location/filename.
Looking at this post I've tried to use the code:
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
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;
}
void downloadFile(const char* url, const char* fname) {
CURL *curl;
FILE *fp;
CURLcode res;
curl = curl_easy_init();
if (curl){
fp = fopen(fname, "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);
curl_easy_cleanup(curl);
fclose(fp);
}
}
int main(void){
downloadFile("http://servera.com/file.txt", "filename.txt");
downloadFile("http://servera.com/file2.txt", "filename2.txt");
downloadFile("http://serverb.com/file1.txt", "filename1b.txt");
return 0;
}
But when I compile it using g++ test.cpp -o new
I get the following errors:
/tmp/cca1J32y.o: In function `downloadFile(char const*, char const*)':
test.cpp:(.text+0x35): undefined reference to `curl_easy_init'
test.cpp:(.text+0x72): undefined reference to `curl_easy_setopt'
test.cpp:(.text+0x8d): undefined reference to `curl_easy_setopt'
test.cpp:(.text+0xa7): undefined reference to `curl_easy_setopt'
test.cpp:(.text+0xb2): undefined reference to `curl_easy_perform'
test.cpp:(.text+0xc0): undefined reference to `curl_easy_cleanup'
collect2: error: ld returned 1 exit status
I have googled to see if I can find what this means, but I haven't found anything I understand.
How do I get this to work ?
Thanks