64

I am trying to use Curl in C.

I visited Curl official page, and copied sample source code.

below is the link: http://curl.haxx.se/libcurl/c/sepheaders.html

when I run this code with command "gcc test.c",

the console shows message like below.

/tmp/cc1vsivQ.o: In function `main':
test.c:(.text+0xe1): undefined reference to `curl_global_init'
test.c:(.text+0xe6): undefined reference to `curl_easy_init'
test.c:(.text+0x10c): undefined reference to `curl_easy_setopt'
test.c:(.text+0x12e): undefined reference to `curl_easy_setopt'
test.c:(.text+0x150): undefined reference to `curl_easy_setopt'
test.c:(.text+0x17e): undefined reference to `curl_easy_cleanup'
test.c:(.text+0x1b3): undefined reference to `curl_easy_cleanup'
test.c:(.text+0x1db): undefined reference to `curl_easy_setopt'
test.c:(.text+0x1e7): undefined reference to `curl_easy_perform'
test.c:(.text+0x1ff): undefined reference to `curl_easy_cleanup'

I do not know how to solve this.

Juneyoung Oh
  • 7,318
  • 16
  • 73
  • 121

4 Answers4

121

You don't link with the library.

When using an external library you must link with it:

$ gcc test.c -lcurl

The last option tells GCC to link (-l) with the library curl.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    @Accountantم If there's only a static `curl` library, it will be linked statically. Otherwise if it's dynamic then it will be linked dynamically. – Some programmer dude Feb 24 '19 at 08:34
38

In addition to Joachim Pileborg's answer, it is useful to remember that gcc/g++ linking is sensitive to order and that your linked libraries must follow the things that depend upon them.

$ gcc -lcurl test.c

will fail, missing the same symbols as before. I mention this because I came to this page for forgetting this fact.

chirality
  • 887
  • 7
  • 12
  • 4
    Could you please tell me why `gcc -lcurl test.c` **will fail** but `gcc test.c -lcurl` worked fine? – Lane Aug 14 '18 at 15:09
  • @Lane The `gcc -lcurl test.c` will fail because gcc links the files in order of input, so it cannot reference - for example `curl_global_init` - in files appear after `test.c`. – Ebrahimi Oct 14 '18 at 05:52
  • 1
    Many thanks to this post!!!! This saved my day. After reading this post I have just changed the order of linking libraries and I could able to compile and link successfully. – Ashish Mittal Nov 20 '18 at 04:28
  • the param order is wrong : gcc test.c -lcurl – Bruno Ribeiro Feb 08 '23 at 21:01
4

I have the same problem, but i use g++ with a make file. This is a linker issue. You need to add option -lcurl on the compiler and on the linker. In my case on the make file:

CC ?= gcc
CXX ?= g++
CXXFLAGS += -I ../src/ -I ./ -DLINUX -lcurl  <- compile option
LDFLAGS += -lrt -lpthread -lcurl      <- linker option

Gerard

Gerard
  • 41
  • 1
0

Depending how bad things are you might need an -L/somewhere in LDFLAGS to let the linker know where the libraries are. ldconfig is supposed to pick them up and find them on every boot but on a new machine it can take a little prodding, like adding a directory to your /etc/ld.so.conf.

Alan Corey
  • 577
  • 6
  • 10