How to Link Libcurl to My C++ Program in Linux

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

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.

Setting up libcurl on Linux

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.

Static linking libcurl using c

Libcurl is itself linked against a ton of other libraries, most of which aren't included in your compile command line. For instance, mine (on Debian Squeeze) links against:

  • libc
  • libcom_err
  • libcrypto
  • libdl
  • libgcrypt
  • libgnutls
  • libgpg-error
  • libgssapi_krb5
  • libidn
  • libk5crypto
  • libkeyutils
  • libkrb5
  • libkrb5support
  • liblber-2.4
  • libldap_r-2.4
  • libpthread
  • libresolv
  • librt
  • libsasl2
  • libssh2
  • libssl
  • libtasn1
  • libz

(You can get a similar list for yourself by running ldd on the library on Linux, or otool -L on Darwin.)

libcurl C++: How to correctly install and use on CentOS 7

You will need to install the libcurl-devel package as it contains the headers files you are missing.

The libcurl-devel package includes header files and libraries
necessary for developing programs which use the libcurl library. It
contains the API documentation of the library, too

C - Cmake compiling program with libcurl

You link with static curl library (libcurl.a) which required CURL_STATICLIB definition. Add this in your CMakeLists.txt between "project" and "add_executable" commands:

add_definitions( -DCURL_STATICLIB )

Moreover, your file .dlla is not .dll.a ? In that case, .dll.a file is (I think) generated by MinGW toolchain and is required only if you link dynamically with curl library (as .dll file).

I downloaded the sources of cURL library here and compiled it (with Visual Compiler 2015). It produces shared libraries (libcurl.dll and libcurl_imp.lib) that I move in the lib directory (I should rename libcurl_imp.lib to curl.lib). I move also the include directory.

Then, I used this CMakeLists.txt :

cmake_minimum_required(VERSION 3.10)
project(curl_test2 C)

set(CMAKE_C_STANDARD 99)

set(SRCS "srcs/main.c")

set( CURL_LIBRARY ${CMAKE_SOURCE_DIR}/lib )
set( CURL_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include )
find_package( CURL )

include_directories( ${CURL_INCLUDE_DIRS} )
link_directories( ${CURL_LIBRARIES} )

add_executable(curl_test2 ${SRCS})
target_link_libraries(curl_test2 curl)

With it, your project compiles and the execution works.

I also tried with cURL static library (for that, if you use CMake to compile cURL, add -DCURL_STATICLIB=ON in cmake command line). And I moved the produced library libcurl.lib to lib\curl.lib.

I used this CMakeLists.txt :

cmake_minimum_required(VERSION 3.10)
project(curl_test2 C)

set(CMAKE_C_STANDARD 99)

set(SRCS "srcs/main.c")
add_definitions( -DCURL_STATICLIB )

set( CURL_LIBRARY ${CMAKE_SOURCE_DIR}/lib )
set( CURL_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include )
find_package( CURL )

include_directories( ${CURL_INCLUDE_DIRS} )
link_directories( ${CURL_LIBRARIES} )

add_executable(curl_test2 ${SRCS})
target_link_libraries(curl_test2 curl wldap32 ws2_32)

And it works too.

How to properly install libcurl and link it to a c++ program?

The error you are getting is a linker error. The linker is not able to find the curl library.
You need to specify paths where the linker would search for appropriate libraries at link time.

in this case (if you have installed lib curl in the standard lib directories e.g. /usr/lib, r /usr/local/lib) the following should work:

g++ you_file_name.cpp -lcurl

Otherwise you have to specify the path to the directory where the library can be found.
for example:

g++ -L/curl/lib/dir -lcurl you_file_name.cpp.

When there are multiple libraries to link to, these things become complicated so it is a good idea to use a build-system such as CMake to assist in managing include-directories/library paths and various other things.



Related Topics



Leave a reply



Submit