Beamer180
Beamer180

Reputation: 1525

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

I have been trying to get a c++ program to use libcurl and can't figure it out. When developing in C++ I usually use visual studio but this project is using vi an ssh session to a centos machine using VI and g++. I have ran yum install curl, yum install libcurl, yuminstall curl-devel and yum install libcurl-devel and still cannot get the program to compile.

The documentation on the API is pretty good and I can find information how to use libcurl once it is properly installed but getting it installed is proving to be a pain in the but.

The code is:

#include<iostream>
#include<string>
#include<curl/curl.h>
using namespace std;


string data; //will hold the urls contents

size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{ //callback must have this declaration
    //buf is a pointer to the data that curl has for us
    //size*nmemb is the size of the buffer

    for (int c = 0; c<size*nmemb; c++)
    {
        data.push_back(buf[c]);
    }
    return size*nmemb; //tell curl how many bytes we handled
}

int main(void) {

 CURL* curl;

    curl_global_init(CURL_GLOBAL_ALL);
    curl=curl_easy_init();

    curl_easy_setopt(curl, CURLOPT_URL, "https://domain.com");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_USERPWD, "username:password");

    curl_easy_perform(curl);

    cout << endl << data << endl;
    cin.get();

    curl_easy_cleanup(curl);
    curl_global_cleanup();


    return 0;
}

I get the following error when I try to compile:

/tmp/ccfeybih.o: In function `main':
helloworld.cpp:(.text+0x72): undefined reference to `curl_global_init'
helloworld.cpp:(.text+0x77): undefined reference to `curl_easy_init'
helloworld.cpp:(.text+0x96): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xb1): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xcc): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xe7): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xf3): undefined reference to `curl_easy_perform'
helloworld.cpp:(.text+0x132): undefined reference to `curl_easy_cleanup'
helloworld.cpp:(.text+0x137): undefined reference to `curl_global_cleanup'
collect2: ld returned 1 exit status

I can't find where to go from here.

Upvotes: 4

Views: 16817

Answers (2)

Michał G&#243;rny
Michał G&#243;rny

Reputation: 19233

The proper way of building against libcurl is through use of pkg-config (like with many modern packages). In case of direct g++ call, that would be:

g++ $(pkg-config --cflags libcurl) helloworld.cpp $(pkg-config --libs libcurl)

This will automatically handle non-common (yet still correct) installations of libcurl where the libraries and headers are not in system include/library directories. Without pkg-config, you will need to always find out the correct path yourself.


If you get into using autotools, you would use in configure.ac:

PKG_CHECK_MODULES([CURL], [libcurl])

And then in Makefile.am:

helloworld_CPPFLAGS = $(CURL_CFLAGS)
helloworld_LIBS = $(CURL_LIBS)

Upvotes: 2

A. K.
A. K.

Reputation: 38098

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.

Upvotes: 6

Related Questions