Wie man richtig installieren libcurl und verbinden Sie es mit einem c++ - Programm?

Habe ich versucht zu bekommen, ein c++ - Programm verwendet libcurl, und es kann nicht herausfinden. Bei der Entwicklung in C++, ich in der Regel verwenden Sie visual studio, aber das Projekt ist mit vi eine ssh-session zu einer centos Maschine mit dem VI und g++. Ich lief yum install curl, yum install libcurl, yuminstall curl-devel und yum install libcurl-devel und noch nicht bekommen, das Programm zu kompilieren.

In der Dokumentation der API ist ziemlich gut und ich kann finden Sie Informationen, wie Sie verwenden, libcurl, sobald es richtig installiert aber immer es installiert ist, erweist sich als ein Schmerz in der aber.

Der code ist:

#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;
}

Bekomme ich die folgende Fehlermeldung, wenn ich versuche zu kompilieren:

/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

Kann ich nicht finden, wo gehen Sie von hier aus.

  • Man muss hinzufügen, verknüpfen fahne in der g++ - Aufruf-Optionen. So etwas wie g++ ... -lcurl. Oder Sie erstellen makefile für Ihr Projekt.
  • Würden Sie geschehen, zu wissen, jeder gute tutorials, wie Sie dies tun?
  • Zum Beispiel, zuerst link von google.
  • Vielen Dank für die Frage, ich hatte das gleiche problem und ich habe Lösungen zu finden für eine lange Zeit.
InformationsquelleAutor Beamer180 | 2012-07-24
Schreibe einen Kommentar