How to use libcurl in qt ?
-
I followed all the steps mentioned in above link though I am getting undefined reference to _imp__cur
My question is do I need to build libcurl source code manually and create dlls which can be used in my project ? Can't I use the binaries which are available in [https://curl.se/download.html] ?
-
@ankit-thakar C libraries are less problematic - you can use a build for your platform.
C++ libraries though require that they are built using same compiler (for example you can't mix MSVC and MinGW). -
@jsulm :- Which means I need to build libcurl code using mingw and then I need to use those generated binaries in my application, right ?
One more question is performance wise is libcurl better or QNetworkAccessManager ?
Let's say if I want to download some files from jenkins server which one will be better ? -
@ankit-thakar said in How to use libcurl in qt ?:
Which means I need to build libcurl code using mingw
Actually not, as libcurl is a C library as far as I know.
What is faster I don't know.Can you post your build log with the error and also your pro file?
-
Actually when I used Qt 5.13.2 mintgw environment it is working but unfortunately my program crashes in curl_easy_perform API.
Below is my .pro file
QT -= gui TARGET = MyCurl CONFIG += c++11 console CONFIG -= app_bundle DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp LIBS+=D:/curl-7.84.0_4-win64-mingw/curl-7.84.0_4-win64-mingw/lib/libcurl.a LIBS+=D:/curl-7.84.0_4-win64-mingw/curl-7.84.0_4-win64-mingw/lib/libcurl.dll.a INCLUDEPATH += D:/curl-7.84.0_4-win64-mingw/curl-7.84.0_4-win64-mingw/include # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
here is my main.cpp
#include <stdio.h> #include <curl/curl.h> #include <QDebug> #include <string> int main(void) { CURL *curl; FILE *fp; CURLcode res; std::string url = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Nusfjord_road%2C_2010_09.jpg/1280px-Nusfjord_road%2C_2010_09.jpg"; char outfilename[FILENAME_MAX] = "D:/ankit.jpg"; curl = curl_easy_init(); if (curl) { fp = fopen(outfilename,"wb"); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); if(res == CURLE_OK) printf("Download Successful."); else printf("Not sucessful"); curl_easy_cleanup(curl); fclose(fp); } return 0; }
-
@ankit-thakar said in How to use libcurl in qt ?:
my program crashes in curl_easy_perform API
Please post the stack trace after the crash
-
-
Make this:
char outfilename[FILENAME_MAX] = "D:/ankit.jpg";
a Windows path:
char outfilename[FILENAME_MAX] = "D:\\ankit.jpg";
The code crashes on my Linux box because fp is null after the file could not be created.
PS: Always check the result of operations that can fail, especially those returning pointers.
-
@ChrisW67 Actually the problem was something else, in libcurl documentation it is mentioned that callback function if you are using win32 dll.
static size_t cb(void *data, size_t size, size_t nmemb, void *userp) { FILE *fp = (FILE*)userp; return fwrite(data, size, nmemb, fp); } curl_easy_setopt(handles[i], CURLOPT_WRITEFUNCTION, cb); curl_easy_setopt(handles[i], CURLOPT_WRITEDATA, fp);
please refer link text