37

I need to use libcurl in a piece of software I am writing on my ubuntu machine. I am using Eclipse to write and compile all of the software. When I put the libcurl files in the same folder as the .cpp file, and include the curl.h file in the header, When I attempt to compile the program, It comes up with these errors:

Building target: sms
Invoking: GCC C++ Linker
g++  -o"sms"  ./src/sms.o   
./src/sms.o: In function `main':
/home/geekman/workspace/sms/Debug/../src/sms.cpp:38: undefined reference to `curl_easy_init'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:42: undefined reference to `curl_easy_setopt'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:44: undefined reference to `curl_easy_setopt'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:46: undefined reference to `curl_easy_perform'
/home/geekman/workspace/sms/Debug/../src/sms.cpp:47: undefined reference to `curl_easy_cleanup'
collect2: ld returned 1 exit status
make: *** [sms] Error 1

I took the contents of the include folder from libcurl, and placed them in the same folder as the .cpp file. then in the header of the .cpp file, I typed:

#include <curl/curl.h>

I also tried:

#include "curl/curl.h"

Any ideas on the problem? Thanks.

Austin Witherspoon
  • 725
  • 2
  • 7
  • 6

7 Answers7

62

Your header file inclusions are just fine; your problem is occurring at the linking step. In order to link against libcurl, you need to add the -lcurl command line option, assuming it's installed in a standard directory:

g++ -o sms ./src/sms.o -lcurl

If it's not installed in a standard directory, you also need to add the -L/path/to/libcurl, e.g. something like:

# Assuming that /home/geekman/workspace/libcurl is where libcurl.a is located
g++ -o sms ./src/sms.o -L/home/geekman/workspace/libcurl -lcurl

Also note that the -lcurl option has to appear after the list of object files you're linking, otherwise it won't link properly.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 1
    Some libcurl functions have additional dependencies, e.g. zlib, which may result some compiler warnings, e.g.http://stackoverflow.com/questions/11606124/undefined-reference-to-inflate Possible dependencies listed at http://curl.haxx.se/docs/libs.html – Donal Lafferty Sep 21 '14 at 13:26
  • Thanks for mentioning that -lcurl must be after the object files... You saved me a lot of time :) – Bence Gedai Nov 25 '15 at 23:08
10

You can try to use curl-config --libs.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
Alex Pantalones
  • 101
  • 1
  • 2
  • 1
    And for `curl-config` to be installed, you may want to install the `curl` development package (`curl-dev` or `curl-devel` or something like that). – Stefan van den Akker Jan 09 '15 at 10:10
5

An alternate answer (the first one is excellent). Consider using the output returned by "pkg-config --libs libcurl" as an argument to your compiler.

For example,

CPPFLAGS=`pkg-config --libs libcurl`

g++ $CPPFLAGS myfile.o

Pkg-config is a standard way for open source libraries to communicate to you how to link against them / #include their files.

Sam
  • 2,939
  • 19
  • 17
3

Anyone who is using ecplise CDT then you need to do following. First on terminal enter

curl-config --libs

On my machine, the result is

-L/usr/lib/i386-linux-gnu -lcurl

then do according to this screenshot and you will be able to compile. btw don't forget to add header files in your code

enter image description here

So you enter library folder path without -L and library name without -l because they will be automatically added by linker.

adeel41
  • 3,123
  • 1
  • 29
  • 25
1

You have to link the library to your program. With gcc (and most other compilers) you can specify the libraries to link with -lname_wo_lib, e.g. -lcurl

Jared Forth
  • 1,577
  • 6
  • 17
  • 32
ckruse
  • 9,642
  • 1
  • 25
  • 25
0

Also see GNU GCC Manual - Options for Linking for a detailed explanation of the options Adam Rosenfield said. For standard search directories, see An Introduction to GCC - for the GNU Compilers gcc and g++ - Setting Search Paths.

0

In addition to the first answer, I had to link the curlpp library too. So to compile the main.cpp file which included the curlpp I had to do:

g++ main.cpp -lcurl -lcurlpp

Using only one of the two links would return different errors regarding different links. It is important to remind that this only worked because I had installed all the necessary libraries in the standard include folders