I have tried to add cURl to my C++ project but I don't know to add third-party libraries especially in Clion. and please don not close the question it's different with this stackoverflow.com/questions/26704629/setting-up-curl-library-path-in-cmake After I downloaded cURL files I have copied its file to my MinGW folder (Bin to Bin, Include to Include,...)
but I have these errors what should I do?!
My code:
#include <curl/curl.h>
#include <string>
#include <string.h>
size_t writeFunction(void *ptr, size_t size, size_t nmemb, std::string* data) {
data->append((char*) ptr, size * nmemb);
return size * nmemb;
}
int main(int argc, char** argv) {
auto curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/whoshuu/cpr/contributors?anon=true&key=value");
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:pass");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.42.0");
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
std::string response_string;
std::string header_string;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_string);
char* url;
long response_code;
double elapsed;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl = NULL;
}
}
CmakeList.txt:
cmake_minimum_required(VERSION 3.16)
project(untitled6)
set(CMAKE_CXX_STANDARD 20)
add_executable(untitled6 main.cpp)
And the errors:
====================[ Build | untitled6 | Debug ]===============================
"C:\Program Files\JetBrains\CLion 2020.1.2\bin\cmake\win\bin\cmake.exe" --build C:\Users\Aschkan\Desktop\untitled6\cmake-build-debug --target untitled6 -- -j 8
[ 50%] Linking CXX executable untitled6.exe
CMakeFiles\untitled6.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Aschkan/Desktop/untitled6/main.cpp:12: undefined reference to `_imp__curl_easy_init'
C:/Users/Aschkan/Desktop/untitled6/main.cpp:14: undefined reference to `_imp__curl_easy_setopt'
C:/Users/Aschkan/Desktop/untitled6/main.cpp:15: undefined reference to `_imp__curl_easy_setopt'
C:/Users/Aschkan/Desktop/untitled6/main.cpp:16: undefined reference to `_imp__curl_easy_setopt'
C:/Users/Aschkan/Desktop/untitled6/main.cpp:17: undefined reference to `_imp__curl_easy_setopt'
C:/Users/Aschkan/Desktop/untitled6/main.cpp:18: undefined reference to `_imp__curl_easy_setopt'
CMakeFiles\untitled6.dir/objects.a(main.cpp.obj):C:/Users/Aschkan/Desktop/untitled6/main.cpp:19: more undefined references to `_imp__curl_easy_setopt' follow
CMakeFiles\untitled6.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Aschkan/Desktop/untitled6/main.cpp:30: undefined reference to `_imp__curl_easy_getinfo'
C:/Users/Aschkan/Desktop/untitled6/main.cpp:31: undefined reference to `_imp__curl_easy_getinfo'
C:/Users/Aschkan/Desktop/untitled6/main.cpp:32: undefined reference to `_imp__curl_easy_getinfo'
C:/Users/Aschkan/Desktop/untitled6/main.cpp:34: undefined reference to `_imp__curl_easy_perform'
C:/Users/Aschkan/Desktop/untitled6/main.cpp:35: undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [untitled6.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/untitled6.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/untitled6.dir/rule] Error 2
mingw32-make.exe: *** [untitled6] Error 2
CMakeFiles\untitled6.dir\build.make:84: recipe for target 'untitled6.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/untitled6.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/untitled6.dir/rule' failed
Makefile:117: recipe for target 'untitled6' failed