使用 libcurl 编译 Windows 的 C 源代码

Clá*_*ael 1 c windows mingw libcurl

使用 mingw32 和 libcurl32 使用 libcurl 编译适用于 Windows 的 C 源代码。命令行如下:

gcc -c -I "c:\curl\include" -L "c:\curl\lib" -o simple.exe simple.c
Run Code Online (Sandbox Code Playgroud)

编译运行成功,生成exe文件。但是当我尝试运行它时,我收到消息:

不受支持的 16 位应用程序。由于与 64 位版本的 Windows 不兼容,程序或功能“simple.exe”无法启动或运行。

gcc(MinGW-W64 i686-ucrt-posix-dwarf,由 Brecht Sanders 构建)12.2.0

curl 7.83.1 (Windows) libcurl/7.83.1 Schannel 发布日期:2022-05-13 协议:dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp 功能:AsynchDNS HSTS IPv6 Kerberos Largefile NTLM SPNEGO SSL SSPI UnixSockets

我的代码:

#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, "https://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;
}
Run Code Online (Sandbox Code Playgroud)

S.M*_*.M. 5

不受支持的 16 位应用程序。由于与 64 位版本的 Windows 不兼容,程序或功能“simple.exe”无法启动或运行。

gcc -c编译目标文件,-o simple.exe用法错误,应该是-o simple.obj-o simple.o. 你得到一个名为 的目标文件simple.exe,目标文件无法运行。

删除-c并添加-lcurl以将可执行文件与 libcurl 链接:

gcc -I "c:\curl\include" -L "c:\curl\lib" -o simple.exe simple.c -lcurl
Run Code Online (Sandbox Code Playgroud)