Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I link libcurl to my c++ program in linux?

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.

like image 1000
Austin Witherspoon Avatar asked Jun 10 '11 05:06

Austin Witherspoon


People also ask

What is libcurl in C?

libcurl is a free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP.

How do I add libcurl to Visual Studio?

Go to lib directory and double click on “curllib. dsw” which is the Visual Studio Project file for curlib. It will start the Visual Studio with correct configuration and settings. Of course you need to have Visual Studio installed on your machine.

How do I download libcurl?

Go to http://curl.haxx.se/download.html and download one of the following zip files: If you have a Windows 64 system, scroll to the Win64 - Generic section and look for the latest Win64 ia64 zip version with SSL support. It's normally second in the list. Click the version number to start the download.


2 Answers

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.

like image 151
Adam Rosenfield Avatar answered Sep 19 '22 03:09

Adam Rosenfield


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

like image 26
Alex Pantalones Avatar answered Sep 20 '22 03:09

Alex Pantalones