【问题标题】:libcurl in Netbeans: 'undefined reference to...'Netbeans 中的 libcurl:'未定义的引用...'
【发布时间】:2011-12-31 00:34:31
【问题描述】:

我已经尝试在 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:\cygwin\lib”,cygcurl-4.dll 位于“E:\cygwin\bin”。

如果有人可以帮助我,那就太好了!

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

【问题讨论】:

标签: c++ c netbeans cygwin libcurl


【解决方案1】:

-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 manual的逐字记录:

在命令中写入此 [-l] 选项会有所不同;这 链接器按顺序搜索和处理库和目标文件 它们是指定的。因此,foo.o -lz bar.o' searches libraryz' 之后 文件 foo.o 但在 bar.o 之前。如果 bar.o 引用了 `z' 中的函数, 这些函数可能无法加载。

【讨论】:

  • @Alexander:一模一样一样吗?
  • 这是有效的:g++.exe -o dist/Debug/Cygwin_4.x_1-Windows/cppapplication_3 -s build/Debug/Cygwin_4.x_1-Windows/main.o -L/usr/local/ lib -lcurl
  • 不客气。无论如何...... - 它只在使用-L/usr/local/lib时有效吗?
  • 好吧,这似乎是合理的...... - 正如你在上面提到的你的libcurl.a/lib 下;-) 顺便说一句:我认为-s 也不是必需的。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 2015-07-03
  • 2023-03-11
  • 2016-07-02
相关资源
最近更新 更多