【问题标题】:how to link a library using gcc in windows 10?如何在 Windows 10 中使用 gcc 链接库?
【发布时间】: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(它有大量的依赖项 - 这不是微不足道的)。

标签: c curl gcc


【解决方案1】:

测试使用'-L' gcc 标志。这将接收 lib 文件所在的目录。

示例:gcc simple.c -L curlDownloadDirectory -lcurl

【讨论】:

    【解决方案2】:

    您需要 libcurl.a(您链接的部分)和 curl.h(您包含的头文件),但您还必须告诉编译器/链接器在哪里查找头文件 (-I &lt;path&gt;) 以及在哪里寻找图书馆(-L)。您可以有多个 -I-L 规范。

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 2019-02-13
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      相关资源
      最近更新 更多