Netbeans 中的 libcurl:'undefined reference to...'

libcurl in Netbeans: 'undefined reference to...'

本文关键字:reference to undefined 中的 Netbeans libcurl      更新时间:2023-10-16

我现在已经尝试了几天在Windows机器上运行Netbeans(编译器是cygwin)的libcurl示例c++代码,但每次都失败。我也通过网络(也通过stackoverflow)搜索过,但找不到解决方案……(

这是我想运行的示例代码…

#include <cstdlib>
#include <curl/curl.h>
using namespace std;
/*
 * 
 */
int main(int argc, char** argv) {
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    }
    return 0;
}

下面是编译结果:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/Alexander/Documents/NetBeansProjects/CppApplication_3'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x_1-Windows/cppapplication_3.exe
make[2]: Entering directory `/cygdrive/c/Users/Alexander/Documents/NetBeansProjects/CppApplication_3'
mkdir -p build/Debug/Cygwin_4.x_1-Windows
rm -f build/Debug/Cygwin_4.x_1-Windows/main.o.d
g++.exe -lcurl   -c -g -MMD -MP -MF build/Debug/Cygwin_4.x_1-Windows/main.o.d -o build/Debug/Cygwin_4.x_1-Windows/main.o main.cpp
mkdir -p dist/Debug/Cygwin_4.x_1-Windows
g++.exe -lcurl    -o dist/Debug/Cygwin_4.x_1-Windows/cppapplication_3 build/Debug/Cygwin_4.x_1-Windows/main.o  
build/Debug/Cygwin_4.x_1-Windows/main.o: In function `main':
/cygdrive/c/Users/Alexander/Documents/NetBeansProjects/CppApplication_3/main.cpp:20: undefined reference to `_curl_easy_init'
/cygdrive/c/Users/Alexander/Documents/NetBeansProjects/CppApplication_3/main.cpp:22: undefined reference to `_curl_easy_setopt'
/cygdrive/c/Users/Alexander/Documents/NetBeansProjects/CppApplication_3/main.cpp:23: undefined reference to `_curl_easy_perform'
/cygdrive/c/Users/Alexander/Documents/NetBeansProjects/CppApplication_3nbproject/Makefile-Debug.mk:61: recipe for target `dist/Debug/Cygwin_4.x_1-Windows/cppapplication_3.exe' failed
make[2]: Leaving directory `/cygdrive/c/Users/Alexander/Documents/NetBeansProjects/CppApplication_3'
nbproject/Makefile-Debug.mk:58: recipe for target `.build-conf' failed
make[1]: Leaving directory `/cygdrive/c/Users/Alexander/Documents/NetBeansProjects/CppApplication_3'
/main.cpp:25: undefined reference to `_curl_easy_cleanup'
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin_4.x_1-Windows/cppapplication_3.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)

libcurl也作为一个包安装在cygwin中…libcurl。a位于"E:cygwinlib",cygcurl-4.dll位于"E:cygwinbin"。

如果有人能帮我,那就太好了!

提前感谢!问候,亚历克斯

-l选项放在链接命令的末尾,如下所示:

g++ -o main main.o -lcurl

那么在你的例子中输出应该是这样的:

g++.exe -o dist/Debug/Cygwin_4.x_1-Windows/cppapplication_3 build/Debug/Cygwin_4.x_1-Windows/main.o -lcurl

来自gcc手册:

在命令中写入[-l]选项会产生影响;的链接器按顺序搜索和处理库和目标文件它们是指定的。因此,foo.o -lz bar.o' searches library z′后文件foo。但是在酒吧之前。如果酒吧。O表示z中的函数,这些函数不能被加载。

相关文章: