libcurl内存GET示例.exe仅能在MinGW64/bin运行的问题求助
问题描述
我是C语言新手,运行libcurl内存GET示例(仅修改URL并添加响应体打印),用命令gcc -o curl_ex curl_inMem.c -lcurl编译生成.exe后,只有放在C:\msys64\mingw64\bin目录才能正常运行。在项目目录或其他目录运行时,提示“无法在动态链接库C:\msys64\mingw64\bin\libidn2-0.dll中找到程序入口点DllMain”,放入mingw64\bin则正常。
示例代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> struct MemoryStruct { char *memory; size_t size; }; static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) { size_t realsize = size * nmemb; struct MemoryStruct *mem = (struct MemoryStruct *)userp; char *ptr = realloc(mem->memory, mem->size + realsize + 1); if(!ptr) { /* out of memory! */ printf("not enough memory (realloc returned NULL)\n"); return 0; } mem->memory = ptr; memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } int main(void) { CURL *curl_handle; CURLcode res; struct MemoryStruct chunk; chunk.memory = malloc(1); /* will be grown as needed by the realloc above */ chunk.size = 0; /* no data at this point */ curl_global_init(CURL_GLOBAL_ALL); /* init the curl session */ curl_handle = curl_easy_init(); /* specify URL to get */ curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=CYYC&hoursBeforeNow=1"); /* send all data to this function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); /* we pass our 'chunk' struct to the callback function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); /* some servers do not like requests that are made without a user-agent field, so we provide one */ curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); /* get it! */ res = curl_easy_perform(curl_handle); /* check for errors */ if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } else { /* * Now, our chunk.memory points to a memory block that is chunk.size * bytes big and contains the remote file. * * Do something nice with it! */ printf("%s\n", chunk.memory); // Print the response body to stdout printf("%lu bytes retrieved\n", (unsigned long)chunk.size); } /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); free(chunk.memory); /* we are done with libcurl, so clean it up */ curl_global_cleanup(); return 0; }
解决方案
- 复制依赖DLL到程序目录:把
C:\msys64\mingw64\bin下的libcurl相关依赖DLL(包括libidn2-0.dll、libcurl.dll、libssl-1_1-x64.dll、libcrypto-1_1-x64.dll等)全部复制到curl_ex.exe所在的项目目录,让程序优先加载同目录的匹配版本DLL。 - 静态编译libcurl:将所有依赖打包进exe,彻底消除DLL依赖问题。修改编译命令为:
若提示缺少静态库,在MSYS2中执行gcc -o curl_ex curl_inMem.c -lcurl -lssl -lcrypto -lidn2 -lz -staticpacman -S mingw-w64-x86_64-libcurl-static安装静态库包。 - 添加mingw路径到系统PATH:将
C:\msys64\mingw64\bin加入系统环境变量的PATH,让系统自动在该目录查找依赖DLL。操作后需重启命令提示符或资源管理器生效。 - 排查DLL版本冲突:用Dependency Walker工具查看
curl_ex.exe实际加载的DLL路径,确认是否有其他目录的旧版本libidn2-0.dll导致冲突,找到后删除或重命名冲突文件。
内容的提问来源于stack exchange,提问作者NotARealProgrammer
相关产品推荐
相关产品推荐

