cURL 错误:未定义的引用
我在寻找将 URL 下载到我的电脑的方法时发现了此线程: 在 C/C++ 中使用 libcurl 下载文件
我下载了 Windows 版的 curl 并合并了所有内容将 3 个文件夹添加到 MinGW 目录中现有的对应文件夹:include、lib 和 bin。
现在,每当我编译代码时,我都会收到以下错误,我知道它们是由于没有链接正确的库而导致的,但在我提供的链接中,它们没有关于链接内容的足够信息:
obj\Debug\main.o
In function main':
D:\CPP Scrap\curl1\main.cpp:11: undefined reference to __imp__curl_easy_init'
D:\CPP Scrap\curl1\main.cpp:13: undefined reference to __imp__curl_easy_setopt'
D:\CPP Scrap\curl1\main.cpp:14: undefined reference to __imp__curl_easy_perform'
D:\CPP Scrap\curl1\main.cpp:17: undefined reference to __imp__curl_easy_cleanup'
=== Build finished: 4 errors, 0 warnings ===
它显示标头 包含了curl/types.h,但我在下载的[最新]版本的curl中找不到它,所以我猜我必须链接到某些东西,问题是,它是什么?
代码:
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
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, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
I've found this thread while looking for a way to download a URL to my pc:
Download file using libcurl in C/C++
I downloaded curl for windows and merged all 3 folders to their existing counterparts in the MinGW directory: include, lib, and bin.
Now, whenever I compile the code, I got the following errors that I know they are due to not linking the proper libraries, but in the link I provided their is no sufficient info on what to link:
obj\Debug\main.o
In function main':
D:\CPP Scrap\curl1\main.cpp:11: undefined reference to __imp__curl_easy_init'
D:\CPP Scrap\curl1\main.cpp:13: undefined reference to __imp__curl_easy_setopt'
D:\CPP Scrap\curl1\main.cpp:14: undefined reference to __imp__curl_easy_perform'
D:\CPP Scrap\curl1\main.cpp:17: undefined reference to __imp__curl_easy_cleanup'
=== Build finished: 4 errors, 0 warnings ===
It shows that the header curl/types.h
is being included but I cannot find it in the [latest] version of curl I downloaded, so I'm guessing I have to link with something, question is, what is it?
Code:
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
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, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道 Windows 的详细信息(我从未使用过该系统,但自 1993 年以来我一直在愉快地使用 GNU/Linux),但您可能需要链接
libcurl
库(也许使用类似libcurl.lib
和/或libcurl.dll
或类似的东西)。但是您是否考虑过使用 GNU/Linux 作为您的开发平台?如果你想使用 C++ & Curl,它很有意义(因为 Linux 有一个很好的免费 C++ 编译器,GCC,而且因为 libcurl 是可能主要是为 Linux 开发的)。浏览系统所有源代码的能力使您能够学到很多东西。
I don't know the details for Windows (I never used that system, but I am using GNU/Linux since 1993 with happiness), but you probably need to link the
libcurl
library (maybe using files likelibcurl.lib
and/orlibcurl.dll
or something similar).But did you consider to use GNU/Linux as your development platform? If you want to use C++ & Curl, it makes a lot of sense (because Linux has a good free C++ compiler, GCC, and because libcurl is probably developed mostly for and on Linux). And the ability to glance into all the source code of your system enables you to learn a lot of things.