安装cURL后未定义的引用

时间:2014-02-03 08:35:50

标签: c++ curl

我正在用C ++编写一个程序(使用带有mingw的netbeans 7.4)。我从互联网上下载了一个使用cURL库的代码文件,所以我设置了cURL然后我尝试运行代码,但它给了我以下错误:

mkdir -p dist/Debug/MinGW_TDM-Windows
g++     -o dist/Debug/MinGW_TDM-Windows/nb-cppmainproject build/Debug/MinGW_TDM-Windows/curldnld.o 
build/Debug/MinGW_TDM-Windows/curldnld.o: In function `main':
F:\Users\Amol-2\Desktop\Imp Docs\C++ apps\NB-CppMainProject/curldnld.c:18: undefined reference to `_imp__curl_easy_init'
F:\Users\Amol-2\Desktop\Imp Docs\C++ apps\NB-CppMainProject/curldnld.c:21: undefined reference to `_imp__curl_easy_setopt'
F:\Users\Amol-2\Desktop\Imp Docs\C++ apps\NB-CppMainProject/curldnld.c:22: undefined reference to `_imp__curl_easy_setopt'
F:\Users\Amol-2\Desktop\Imp Docs\C++ apps\NB-CppMainProject/curldnld.c:23: undefined reference to `_imp__curl_easy_setopt'
F:\Users\Amol-2\Desktop\Imp Docs\C++ apps\NB-CppMainProject/curldnld.c:24: undefined reference to `_imp__curl_easy_perform'
F:\Users\Amol-2\Desktop\Imp Docs\C++ apps\NB-CppMainProject/curldnld.c:26: undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW_TDM-Windows/nb-cppmainproject.exe] Error 1
make.exe[2]: Leaving directory `/f/Users/Amol-2/Desktop/Imp Docs/C++ apps/NB-CppMainProject'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/f/Users/Amol-2/Desktop/Imp Docs/C++ apps/NB-CppMainProject'
make.exe": *** [.build-impl] Error 2

代码:

#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string.h>

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(void) {
    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url = "http://localhost/aaa.txt";
    char outfilename[FILENAME_MAX] = "C:\\bbb.txt";
    curl = curl_easy_init();
    if (curl) {
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/aaa.txt");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        /* always cleanup */
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    return 0;
}

我试图找到其他资源,但我无法解决问题。我做错了什么?

另外,如果不使用cURL库,我应该怎么做?

1 个答案:

答案 0 :(得分:1)

您尚未关联curl库。也就是说,您的g++来电缺少-lcurl(可能还有-Lpath_to_directory_with_curl_library)。