2013-11-25 148 views
0

我正在嘗試構建從ftp下載文件的捲曲庫示例程序。我正在使用Eclipse IDE,OS - Ubuntu鏈接捲曲庫問題

我已經安裝使用捲曲命令: 易於得到的/usr/include/curl安裝libcurl4-gnutls-dev

我看到heades文件(我不知道哪裏是C文件)

看起來Eclipse是幸福with #include <curl/curl.h>,所有BU在程序中使用的捲曲函數使用'undefined reference'

編譯failes與鏈接錯誤:

**** Build of configuration Debug for project updDown **** 

make all 
Building target: updDown 
Invoking: GCC C Linker 
gcc -o "updDown" ./src/updDown.o 
./src/updDown.o: In function `main': 
/home/g/proj/updDown/Debug/../src/updDown.c:45: undefined reference to `curl_global_init' 
/home/g/proj/updDown/Debug/../src/updDown.c:47: undefined reference to `curl_easy_init' 
/home/g/proj/updDown/Debug/../src/updDown.c:52: undefined reference to `curl_easy_setopt' 
/home/g/proj/updDown/Debug/../src/updDown.c:55: undefined reference to `curl_easy_setopt' 
/home/g/proj/updDown/Debug/../src/updDown.c:57: undefined reference to `curl_easy_setopt' 
/home/g/proj/updDown/Debug/../src/updDown.c:60: undefined reference to `curl_easy_setopt' 
/home/g/proj/updDown/Debug/../src/updDown.c:62: undefined reference to `curl_easy_perform' 
/home/g/proj/updDown/Debug/../src/updDown.c:65: undefined reference to `curl_easy_cleanup' 
/home/g/proj/updDown/Debug/../src/updDown.c:76: undefined reference to `curl_global_cleanup' 
collect2: error: ld returned 1 exit status 
make: *** [updDown] Error 1 

**** Build Finished **** 

如何解決這個問題呢?

Hwole代碼:

/* 
============================================================================ 
Name  : updDown.c 
Author  : 
Version  : 
Copyright : Your copyright notice 
Description : Hello World in C, Ansi-style 
============================================================================ 
*/ 

#include <stdio.h> 
#include <stdlib.h> 
#include <curl/curl.h> 

struct FtpFile { 
    const char *filename; 
    FILE *stream; 
}; 

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) 
{ 
    struct FtpFile *out=(struct FtpFile *)stream; 
    if(out && !out->stream) { 
    /* open file for writing */ 
    out->stream=fopen(out->filename, "wb"); 
    if(!out->stream) 
     return -1; /* failure, can't open file to write */ 
    } 
    return fwrite(buffer, size, nmemb, out->stream); 
} 


int main(void) 
{ 

    puts("starting"); 

    CURL *curl; 
    CURLcode res; 
    struct FtpFile ftpfile={ 
    "curl.tar.gz", /* name to store the file as if succesful */ 
    NULLs 
    }; 

    curl_global_init(CURL_GLOBAL_DEFAULT); 

    curl = curl_easy_init(); 
    if(curl) { 
    /* 
    * You better replace the URL with one that works! 
    */ 
    curl_easy_setopt(curl, CURLOPT_URL, 
        "ftp://ftp.example.com/pub/www/utilities/curl/curl-7.9.2.tar.gz"); 
    /* Define our callback to get called when there's data to be written */ 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); 
    /* Set a pointer to our struct to pass to the callback */ 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); 

    /* Switch on full protocol/debug output */ 
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 

    res = curl_easy_perform(curl); 

    /* always cleanup */ 
    curl_easy_cleanup(curl); 

    if(CURLE_OK != res) { 
     /* we failed */ 
     fprintf(stderr, "curl told us %d\n", res); 
    } 
    } 

    if(ftpfile.stream) 
    fclose(ftpfile.stream); /* close the local file */ 

    curl_global_cleanup(); 

    return 0; 
} 

回答

1

您應指定-lcurl鏈接器選項,在C/C++ build -> Settings -> GCC C Linker -> Libraries在項目屬性的位置。

編輯:

讓我們更詳細。

我看到heades文件在/ usr/include中/捲曲(我不知道哪裏是C文件)

通常這樣的包不包括源代碼,而不是他們提供的預編譯的目標文件名爲libraries.bu所有在程序中使用的curl函數都使用'undefined reference'。

看起來Eclipse是快樂的用#include

這是真的,因爲路徑/usr/include/通常包含路徑(如編譯器試圖找到頭文件)裏面,這樣你就不需要爲此設置任何東西。

bu程序中使用的所有捲曲函數都使用「未定義參考」。

在你使用像curl_global_init功能,而無需自己執行這些代碼,這意味着這些函數被看作是預計將引入的外部功能。你應該「告訴」鏈接器在哪裏找到這些功能(更準確地說,是這些符號)。這是通過使用選項-l,然後是庫名稱完成的。並且爲了指定庫的路徑,使用-L

欲瞭解更多信息,你可以看到在Option Sammary of GCC

+0

沒錯的鏈接器選項的部分。未定義的引用錯誤意味着鏈接器沒有在代碼的任何位置或任何指定的庫中找到該函數的定義。 –