4

私はlibcurlを使用するC++プログラムを取得しようとしてきましたが、それを理解できません。C ++で開発するとき、私は通常Visual Studioを使用しますが、このプロジェクトでは、VIとg++を使用するcentosマシンへのsshセッションをviで使用しています。yum install curl、yum install libcurl、yuminstall curl-devel、yum install libcurl-develを実行しましたが、プログラムをコンパイルできません。

APIに関するドキュメントは非常に優れており、適切にインストールされた後のlibcurlの使用方法に関する情報を見つけることができますが、インストールするのは面倒です。

コードは次のとおりです。

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

コンパイルしようとすると、次のエラーが発生します。

/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

ここからどこへ行けばいいのかわからない。

4

2 に答える 2

6

発生するエラーはリンカーエラーです。リンカはcurlライブラリを見つけることができません。リンク時にリンカーが適切なライブラリを検索するパスを指定する必要があります。

この場合(/ usr / lib、r / usr / local /libなどの標準libディレクトリにlibcurlをインストールした場合)、以下が機能するはずです。

g ++ you_file_name.cpp -lcurl

それ以外の場合は、ライブラリを見つけることができるディレクトリへのパスを指定する必要があります。例えば:

g ++ -L / curl / lib / dir-lcurlyou_file_name.cpp。

リンクするライブラリが複数ある場合、これらは複雑になるため、CMakeなどのビルドシステムを使用して、インクルードディレクトリ/ライブラリパスなどの管理を支援することをお勧めします。

于 2012-07-24T07:29:32.677 に答える
2

libcurlに対してビルドする適切な方法は、pkg-config(多くの最新のパッケージのように)を使用することです。直接g++電話の場合、次のようになります。

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

これにより、ライブラリとヘッダーがシステムのインクルード/ライブラリディレクトリにない、一般的ではない(まだ正しい)libcurlのインストールが自動的に処理されます。pkg-configがないと、常に正しいパスを自分で見つける必要があります。


autotoolsを使い始める場合は、次の場所で使用しconfigure.acます。

PKG_CHECK_MODULES([CURL], [libcurl])

そしてMakefile.am

helloworld_CPPFLAGS = $(CURL_CFLAGS)
helloworld_LIBS = $(CURL_LIBS)
于 2012-07-24T07:34:06.467 に答える