-2

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

Community
  • 1
  • 1
Rocket
  • 1,065
  • 2
  • 21
  • 44

5 Answers5

1

You need to add the curl library to link against. Most probably adding a -lcurl option to your compiler call will do the job.

Hellmar Becker
  • 2,824
  • 12
  • 18
1

First of all, that code is not C++, but C. Those are different languages, that aren't really compatible. There's very very many places on SO where that is discussed, so I won't go into detail here. You #include <iostream>, but don't use it anywhere.

Then, what you're seeing are linker errors, because you use the curl library, but don't tell the linker to use the library to resolve the function names to actual symbols that can be called.

try with -lcurl; again, this is C, and happens to be valid C++, too. You could compile this (if you remove the superfluous #include <iostream>) with gcc instead of g++.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
1

You're correctly including the header for libcurl, but you also need to explicitly link against the dynamic library. You probably need to compile as follows:

g++ test.cpp -o new -lcurl

where -lcurl tells the linker to link against the dynamic library called libcurl.so (on Unix systems).

Taking a look at the Using libcurl page, it actually provides a tool called curl-config that can help you with building the correct compiler command line. On most Unix/Linux shells, you can use it as follows:

g++ $(curl-config --cflags --libs) test.cpp -o new

curl-config --cflags --libs will output the correct command-line options. Placing that command in backticks or $(...) means that this output gets substituted onto your command line, effectively adding the command-line options to your command line.

mindriot
  • 5,413
  • 1
  • 25
  • 34
0

You need to link against the curl library. Add -lcurl to the end of your invocation, something like g++ -o program program.cpp -lcurl.

0

You need to link the libcurl into your program. Use the -l argument to tell the linker which libraries you need. (Skip the "lib" of the library name) g++ test.cpp -o new -lcurl