Juneyoung Oh
Juneyoung Oh

Reputation: 7652

undefined reference to curl_global_init, curl_easy_init and other function(C)

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.

Upvotes: 65

Views: 110703

Answers (6)

indi analytica
indi analytica

Reputation: 1

CXX = g++
CXXFLAGS = -std=c++11 -Wall -Wextra -lcurl
LDFLAGS = -lcurl


SRCDIR = src
INCDIR = includes

SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(SRCS:.cpp=.o)
DEPS = $(wildcard $(INCDIR)/*.h)


**#order of flags passed to the GCC compiler can sometimes be sensitive, #especially when it comes to linker flags (flags starting with -l).
#e.g gcc -lcurl test.c will fail but gcc test.c -lcurl so add cflgs add last**

main: $(OBJS)
    $(CXX)  -o $@ $^ $(CXXFLAGS)

%.o: $(SRCDIR)/%.cpp $(DEPS)
    $(CXX) $(CXXFLAGS) -c -o $@ $<

clean:
    rm -f main $(SRCDIR)/*.o

Upvotes: 0

habi
habi

Reputation: 163

I had the same problem and found another line among it : "libcurl-x86.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86'" which means the lib was 64 bit, while i was compiling for 32 bit.

Upvotes: 0

Alan Corey
Alan Corey

Reputation: 611

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.

Upvotes: 0

Gerard
Gerard

Reputation: 41

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

Upvotes: 4

chirality
chirality

Reputation: 937

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.

Upvotes: 42

Some programmer dude
Some programmer dude

Reputation: 409176

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.

Upvotes: 126

Related Questions