6

I'm trying to compile a c++ sample in Linux(ubuntu) using curl lib but I'm getting undefined reference to 'curl_easy_init'

Compile command:

gcc -L/usr/local/lib -lcurl -I/usr/local/include -o request request.cpp

result:

/tmp/ccZwDiCf.o: In function 'main':<br>
request.cpp:(.text+0xa): undefined reference to 'curl_easy_init'<br>
request.cpp:(.text+0x31): undefined reference to 'curl_easy_setopt'<br>
request.cpp:(.text+0x3d): undefined reference to 'curl_easy_perform'<br>
request.cpp:(.text+0x54): undefined reference to 'curl_easy_strerror'<br>
request.cpp:(.text+0x7b): undefined reference to 'curl_easy_cleanup'<br>
collect2: ld returned 1 exit status

Code:

#include <curl/curl.h>
#include <stdio.h>

int main(int argc, char* argv[]){
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
        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);
    }
    return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Dhiogo Brustolin
  • 69
  • 1
  • 1
  • 4
  • 13
    Put the `-lcurl` at the end of the compiler command. See http://stackoverflow.com/questions/9966959/noobish-linker-errors-when-compiling-against-glib/9966989#9966989 – hmjd Aug 09 '12 at 13:28
  • 2
    Also note that you don't need `-L/usr/local/lib` or `-I/usr/local/include` – Paul R Aug 09 '12 at 13:33
  • 2
    also if its gonna be c++ code in there as you suggest you should use g++, not gcc – Rolle Aug 09 '12 at 14:17
  • 1
    if you're linking curl statically (curl.a), you need to define `CURL_STATICLIB` before including the curl headers; not sure if this is your issue. – Woodrow Douglass Aug 09 '12 at 14:40
  • 1
    Thanks everyone. Just using -l at the end of the command works just fine. – Dhiogo Brustolin Aug 09 '12 at 17:45

1 Answers1

4

Answer just to close this ask.

For resumate, you had to place the -lcurl option at the end of your compilation command line.

hogren
  • 103
  • 6