8

use libcurl to writer some test code. when try to compile, it says undefined reference. already use -lcurl or -L compile option.

root@ubuntu:~/work/test/curlTest# curl-config --libs
-L/usr/lib/x86_64-linux-gnu -lcurl
root@ubuntu:~/work/test/curlTest# gcc -L/usr/lib/x86_64-linux-gnu -lcurl curl.c -o curl
/tmp/ccnFnpaW.o: In function `main':
curl.c:(.text+0xb1): undefined reference to `curl_global_init'
curl.c:(.text+0xbc): undefined reference to `curl_easy_init'
curl.c:(.text+0x109): undefined reference to `curl_easy_setopt'
curl.c:(.text+0x136): undefined reference to `curl_easy_setopt'
curl.c:(.text+0x145): undefined reference to `curl_easy_perform'
collect2: error: ld returned 1 exit status
1
  • already tried, same result
    – hkx_1030
    Jan 27, 2015 at 7:53

1 Answer 1

20

-lcurl should be put in the end of gcc command.

gcc -L/usr/lib/x86_64-linux-gnu curl.c -o curl -lcurl
3
  • For me it was enough to simply move the -l option to the end of the command
    – zpon
    Jan 30, 2016 at 9:37
  • Great, it worked for me. But I still don't know why the position of -lcurl matters. Why it failed before -o but work fine after -o?
    – Lane
    Aug 14, 2018 at 3:34
  • 1
    It is because gcc linking is order-sensitive and that your linked libraries must follow the things that depend on.
    – behkod
    Jun 25, 2019 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.