You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Mingw32下链接libcurl出现未定义引用问题求助

Mingw32下链接静态libcurl.a出现未定义引用问题

我从curl官网下载了适用于32位Windows的静态库libcurl.a,在Mingw32环境下编译无报错,但链接阶段抛出未定义引用错误。

初始错误信息

未定义引用curl_global_init、curl_easy_init、curl_easy_setopt等libcurl核心函数。

POST请求代码

void send_post_request(char* address, char* text, int text_size, char* exe_type, char* auth_key)
{
    // Initialize all sub modules because we're on Windows and might want HTTPS down the line
    curl_global_init(CURL_GLOBAL_ALL);

    // Init a single thread CURL "command instance" - or at least I like to think of it as a command instance because that's how it is on Kali
    CURL* curl_instance = curl_easy_init(); 

    // Set URL - address usually is a macro that is passed in by get_req.js at compile time
    curl_easy_setopt(curl_instance, CURLOPT_URL, address);

    // Set headers
    struct curl_slist* headers = NULL;

    char* to_send = malloc(5 + strlen(exe_type) + 1);       // EXE type  - Add 1 for null byte
    strcpy(to_send, "exe: ");
    strcat(to_send, exe_type);
    headers = curl_slist_append(headers, to_send);

    headers = curl_slist_append(headers, "Content-Type: text/plain");         // Content Type
    
    // Auth
    char* auth_send = malloc(15 + strlen(auth_key) + 1);
    strcpy(auth_send, "authorization: ");
    strcat(auth_send, auth_key);
    headers = curl_slist_append(headers, auth_send);

    // Set headers
    curl_easy_setopt(curl_instance, CURLOPT_HTTPHEADER, headers);

    // Set data to send
    curl_easy_setopt(curl_instance, CURLOPT_POSTFIELDS, text);
    curl_easy_setopt(curl_instance, CURLOPT_POSTFIELDSIZE, text_size);
    
    // Send the POST request
    curl_easy_perform(curl_instance);

    // Cleanup everything (and the POST request)
    free(to_send);
    free(auth_send);
    curl_slist_free_all(headers);
    curl_easy_cleanup(curl_instance);
    curl_global_cleanup();
}

目录结构

-- Libcurl --- libcurl.a
           --- 所有curl头文件

-- Post    --- post.c
           --- post.h
           --- post.o

编译与链接命令

编译阶段(无报错):

gcc -c ./post.c -o post.o -I../Libcurl/ -I./

链接阶段(触发错误):

gcc .\post.o -L../Libcurl/ -lcurl

已尝试的操作

添加-DCURL_STATICLIB编译标志后,原libcurl函数的未定义引用消失,但出现新的错误:未定义引用WSAStartup、CryptAcquireContextA等函数,涉及ws2_32、crypt32、wldap32等系统库。

恳请提供解决建议。

内容的提问来源于stack exchange,提问作者Justin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 17:51:04