【发布时间】:2021-02-06 15:00:09
【问题描述】:
使用 gcc 编译器在 Windows 10 上编译 c 程序时如何将库与文件链接
在阅读其他堆栈溢出问题时,我遇到了带有 curl.h 头文件的 curl 库的 this 下载链接
当我尝试编译文件时出现此错误
C:\Users\0000\Desktop>gcc simple.c -lcurl
simple.c:2:10: fatal error: curl/curl.h: No such file or directory
2 | #include <curl/curl.h>
| ^~~~~~~~~~~~~
compilation terminated.
我也试过这条线
gcc simple.c
也试过了,但我一直收到这个错误
cl simple.c
这显然是一个库链接问题
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* 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);
}
return 0;
}
【问题讨论】:
-
“这显然是一个库链接问题”——不,不是。看看stackoverflow.com/questions/25147295/…,它简要介绍了编译过程的各个部分。查看头文件的使用位置。 (第 1 步),查看调用链接器的位置(第 4 步)。错误是编译器找不到头文件并停止。查看 gcc 的“-I”标志或 cl 的“/I”标志,看看如何解决这个问题。
-
" 但我一直收到此错误":什么错误 - 这不是错误,它看起来像是在尝试使用不同的编译器。
-
你真的有图书馆吗?如果您使用的是 MinGW/gcc:packages.msys2.org/package/mingw-w64-x86_64-curl(它有大量的依赖项 - 这不是微不足道的)。