首页 > 解决方案 > 在 CLion/Mingw 上未定义对“curl_easy_init”的引用

问题描述

我正在尝试在 C++ 代码中使用 curl。我应该怎么做?

我已将 libcurl.a 和 libcurl.dll.a 粘贴到此目录中:C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib

这是我来自 CLion/Mingw 的 CMakeLists.txt

cmake_minimum_required(VERSION 3.3) 
project(tan)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -lcurl.dll")
add_executable(tan Tan/mail.cpp)
project(untitled6)

当我添加“#define CURL_STATICLIB”时,我收到以下错误:

reference to `curl_easy_init'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:84: undefined  
reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:86: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:91: undefined reference to `curl_slist_append'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:92: undefined reference to `curl_slist_append'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:93: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:98: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:99: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:100: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:103: undefined reference to `curl_easy_perform'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:107: undefined reference to `curl_easy_strerror'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:111: undefined reference to `curl_slist_free_all'
C:/Users/John/CLionProjects/tan/Tam/mail.cpp:121: undefined reference to ````curl_easy_cleanup

Without "#define CURL_STATICLIB" i get these errors:

CMakeFiles\tan.dir/objects.a(mail.cpp.obj): In function `main':
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:81: undefined reference to `__imp_curl_easy_init'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:84: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:86: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:91: undefined reference to `__imp_curl_slist_append'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:92: undefined reference to `__imp_curl_slist_append'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:93: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:98: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:99: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:100: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:103: undefined reference to `__imp_curl_easy_perform'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:107: undefined reference to `__imp_curl_easy_strerror'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:111: undefined reference to `__imp_curl_slist_free_all'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:121: undefined reference to `__imp_curl_easy_cleanup'

标签: c++curlcmakeclionmingw-w64

解决方案


如果有人来看,请在CMakeLists.txt. 最后一行解决了这个问题。

步骤1: Copy libcurl.dll.a in C:/curl. No need to copy "libcurl.a" OR "libcurl.dll.a" at any other place. No need for #define CURL_STATICLIB in main.cpp file

Step2:Edit the CMakeLists.txt如下。

set(CMAKE_CXX_STANDARD 14)                                     
add_executable(tan Tan/mail.cpp)
project(untitled6)
target_link_libraries(tan "C:/curl/libcurl.dll.a")

Step3:如果您的程序快速闪烁并关闭。您可能缺少一些 dll 文件。要检查您缺少哪些程序,请使用 cmd 运行程序(在我的情况下为 tan.exe)。从 curl 包中获取该 dll(在我的情况下为 curl-7.65.0-win64-mingw)并将其放在可执行文件的一侧。确保 curl 和您的可执行文件是使用相同的编译器编译的。在我的情况下,它是Mingw。


推荐阅读