在windows上使用curl
在编译的时候出现
fatal error C1083: Can not open include file: “curl / curl.h”: No such file or directory
解决:
需要连接lib和头文件
使用mingw-w64编译
然后按照下图链接lib和头文件
gcc -o xxx 输出名字xxx.exe的可执行文件
-I(大写的i)后面跟着的是curl/curl.h头文件的位置
-L(大写的l)后面跟着的是libcurl.a的位置
-lcurl 是-l(小写的L)+curl(也就是查找libcurl)
@@有可能需要做:把.\curl-7.78.0-win64-mingw\bin,也就是你的curl.exe和libcurl-x64所在的路径在系统全局变量中的path里添加上
成功!!
下面是curl官方下载的样本代码http-post.c
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://www.bilibili.com");
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}