(我知道这个问题已被多次询问,但我似乎有一个特例)
我正在使用gcc(下面的版本信息)来编译一个C ++程序,该程序使用libcURL来擦除硬编码的URL。我在Windows 10上运行它。
> gcc -v
Using built-in specs.
COLLECT_GCC=C:\Windows\MinGW\bin\gcc.exe
COLLECT_LTO_WRAPPER=c:/windows/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
我找到的所有libcURL包都有一个要编译的dll文件。但是,我不想使用该DLL,因为网上的所有示例都使用了头文件,我希望这是一个独立的可执行文件。所以我最终下载了源文件(下载向导链接:https://curl.haxx.se/dlwiz/?type=source&os=-)
当我编译文件时,我最终得到了未定义的引用错误。在我没有链接正确的.lib文件之前,我遇到了这个错误。除此之外,cURL目录中没有任何lib文件。其他一些SO问题据说使用-lcurl链接,但在任何地方都没有curl.lib。
我是否需要购买新包装?或者我需要安装libcURL吗?或其他什么?
代码:
#include <cstddef>
#include <string>
#include "curl-7.58.0\include\curl\curl.h"
std::string gout;
size_t write_data(char *buffer, size_t size, size_t nmemb, void *usrd){
gout += buffer;
return size*nmemb;
}
int geturl(const char* url, std::string out){
gout = "";
out = "";
CURL* hcurl = curl_easy_init();
if(hcurl) {
CURLcode res;
curl_easy_setopt(hcurl, CURLOPT_URL, (char*)url);
curl_easy_setopt(hcurl, CURLOPT_WRITEFUNCTION, write_data);
res = curl_easy_perform(hcurl);
if(res != 0){
curl_easy_cleanup(hcurl);
return -1;
}
curl_easy_cleanup(hcurl);
return 0;
}else{
curl_easy_cleanup(hcurl);
return -1;
}
return -1;
}
int main(){return 0;}
使用gcc编译时会出现错误消息:
> gcc ./file.cpp -o ./test.exe
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0x12): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0x35): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0x49): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0x51): undefined reference to `_imp__curl_easy_init'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0x76): undefined reference to `_imp__curl_easy_setopt'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0x93): undefined reference to `_imp__curl_easy_setopt'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0xa0): undefined reference to `_imp__curl_easy_perform'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0xb6): undefined reference to `_imp__curl_easy_cleanup'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0xca): undefined reference to `_imp__curl_easy_cleanup'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0xde): undefined reference to `_imp__curl_easy_cleanup'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0x109): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
C:\Users\user~1\AppData\Local\Temp\cc6XcEeS.o:checkit-proto.cpp:(.text+0x12a): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
collect2.exe: error: ld returned 1 exit status