Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up libcurl on Linux

Tags:

c

linux

curl

I'm trying to use libcurl, but am failing to set it up correctly. I've been reading the documentation for the past hours, but I'm confused and lost. (This is my first time using an external library with C)

Based on these instructions, I've correctly configured and installed libcurl and curl-config. A minimal C program that simply includes <curl/curl.h> compiles; however, when I run any example program (say, chkspeed.c), I get the following "undefined" errors.

/tmp/ccprXNBB.o: In function `main':
chkspeed.c:(.text+0x1bf): undefined reference to `curl_version'
chkspeed.c:(.text+0x408): undefined reference to `curl_global_init'
chkspeed.c:(.text+0x40d): undefined reference to `curl_easy_init'
chkspeed.c:(.text+0x432): undefined reference to `curl_easy_setopt'
chkspeed.c:(.text+0x454): undefined reference to `curl_easy_setopt'
chkspeed.c:(.text+0x476): undefined reference to `curl_easy_setopt'
chkspeed.c:(.text+0x482): undefined reference to `curl_easy_perform'
chkspeed.c:(.text+0x4b0): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x50b): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x566): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x5c9): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x624): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x66a): undefined reference to `curl_easy_strerror'
chkspeed.c:(.text+0x696): undefined reference to `curl_easy_cleanup'
chkspeed.c:(.text+0x69b): undefined reference to `curl_global_cleanup'
collect2: error: ld returned 1 exit status

The following is my output for the three curl-config flags featured in this guide. I'm not sure how to use this information:

$: curl-config --cflags
-I/usr/local/include
$: curl-config --libs
-L/usr/local/lib -lcurl
$: curl-config --feature
IPv6
UnixSockets
libz
AsynchDNS

I would really appreciate any help that might get me in the right direction, if not solve the issue. Thank you for your time!

like image 754
ZM- Avatar asked Feb 28 '26 01:02

ZM-


1 Answers

You should compile it like this:

$ gcc chkspeed.c -o chkspeed $(curl-config --cflags) $(curl-config --libs)

so that the gcc command can have the proper CFLAGS and LDFLAGS for compiling and linking against libcurl.

Note when working with a shell (like bash) and you execute a command like this:

$ cmd1 arg1 arg2 $(cmd2 arg3)

the shell will evaluate first cmd arg3 by executing it and using the stdout output of cmd2 as an argument of for cmd1. Let's say that cmd2 arg3 prints (on stdout) hello, then the shell will execute cmd1 arg1 arg2 hello.

So

$ gcc chkspeed.c -o chkspeed $(curl-config --cflags) $(curl-config --libs)

will be executed as

$ gcc chkspeed.c -o chkspeed -I/usr/local/include -L/usr/local/lib -lcurl

because the output of curl-config --cflags is -I/usr/local/include and the output of curl-config --libs is -L/usr/local/lib -lcurl.

like image 120
Pablo Avatar answered Mar 01 '26 18:03

Pablo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!