我现在已经尝试过在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'。
如果有人可以帮助我,那就太棒了!
提前致谢! 问候, 亚历
答案 0 :(得分:0)
将-l
选项放在link命令的末尾,如下所示:
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
逐字逐句
在命令中你写这个[-l]选项会有所不同;该 链接器按顺序搜索和处理库和目标文件 它们是指定的。因此,
foo.o -lz bar.o' searches library
z'之后 文件foo.o但在bar.o之前如果bar.o引用'z'中的函数, 这些功能可能无法加载。