将libcurl包含到Microsoft Visual Studio 2015中 [英] Include libcurl to Microsoft Visual Studio 2015
问题描述
我不知道如何在我的项目中包含curl。我已阅读将LibCurl与Visual Studio 2013结合使用,但在我的文件夹中带有卷曲(来自 https://curl.haxx.se/download。 html#Win64 -Win64 x86_64 7zip)仅在bin文件夹中 libcurl.dll 。在lib文件夹中,只有 libcurl.a 和 libcurldll.a 。
I don't have any idea how to include curl to my project. I've read this Getting LibCurl to work with Visual Studio 2013, but in my folder with curl (from https://curl.haxx.se/download.html#Win64 - Win64 x86_64 7zip) is only libcurl.dll in bin folder. In lib folder there is only libcurl.a and libcurldll.a.
我添加了包含路径 VC ++目录-> Inlcude目录,并尝试编译以下代码:
I've added include path to VC++ Directories -> Inlcude Directories, and try to compile this code:
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
struct url_data {
size_t size;
char* data;
};
size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) {
size_t index = data->size;
size_t n = (size * nmemb);
char* tmp;
data->size += (size * nmemb);
#ifdef DEBUG
fprintf(stderr, "data at %p size=%ld nmemb=%ld\n", ptr, size, nmemb);
#endif
tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */
if(tmp) {
data->data = tmp;
} else {
if(data->data) {
free(data->data);
}
fprintf(stderr, "Failed to allocate memory.\n");
return 0;
}
memcpy((data->data + index), ptr, n);
data->data[data->size] = '\0';
return size * nmemb;
}
char *handle_url(char* url) {
CURL *curl;
struct url_data data;
data.size = 0;
data.data = malloc(4096); /* reasonable size initial buffer */
if(NULL == data.data) {
fprintf(stderr, "Failed to allocate memory.\n");
return NULL;
}
data.data[0] = '\0';
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
return data.data;
}
int main(int argc, char* argv[]) {
char* data;
if(argc < 2) {
fprintf(stderr, "Must provide URL to fetch.\n");
return 1;
}
data = handle_url(argv[1]);
if(data) {
printf("%s\n", data);
free(data);
}
return 0;
}
但我遇到这样的错误:
在函数handle_url
推荐答案
您必须链接针对libcurl.a(用于静态链接)或libcurldll.a(用于动态链接)的应用程序。
You have to link your application against a libcurl.a (for static linking) or libcurldll.a (for dynamic linking).
首先,您应该在项目设置中添加一个附加的库目录,以便指向包含这些.a文件的文件夹,然后有两个选项:
First you should add an additional library directory in a project settings that points to a folder that contains these .a files and after that there are two options:
要在项目设置中添加依赖项...
To add a dependency in a project settings...
Project Properties -> Linker -> Input -> Additional Dependencies.
...或使用预处理器(只需将此代码行添加到源文件中):
...or to use a preprocessor (just add this line of code to a source file):
#pragma comment(lib, "libcurl.a")
祝你好运!
这篇关于将libcurl包含到Microsoft Visual Studio 2015中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!