Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use libcurl in qt ?
Forum Updated to NodeBB v4.3 + New Features

How to use libcurl in qt ?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 4.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ankit thakar
    wrote on 11 Jul 2022, 09:30 last edited by
    #1

    Re: cURL and Qt Project

    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] ?

    J 1 Reply Last reply 11 Jul 2022, 10:33
    0
    • A ankit thakar
      11 Jul 2022, 09:30

      Re: cURL and Qt Project

      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] ?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 11 Jul 2022, 10:33 last edited by
      #2

      @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).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 11 Jul 2022, 10:41
      0
      • J jsulm
        11 Jul 2022, 10:33

        @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).

        A Offline
        A Offline
        ankit thakar
        wrote on 11 Jul 2022, 10:41 last edited by ankit thakar 7 Nov 2022, 10:42
        #3

        @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 ?

        J 1 Reply Last reply 11 Jul 2022, 10:49
        0
        • A ankit thakar
          11 Jul 2022, 10:41

          @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 ?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 11 Jul 2022, 10:49 last edited by
          #4

          @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?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ankit thakar
            wrote on 11 Jul 2022, 13:17 last edited by
            #5

            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;
            }
            
            
            J 1 Reply Last reply 11 Jul 2022, 14:27
            0
            • A ankit thakar
              11 Jul 2022, 13:17

              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;
              }
              
              
              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 11 Jul 2022, 14:27 last edited by
              #6

              @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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ankit thakar
                wrote on 12 Jul 2022, 09:40 last edited by
                #7

                Capture.JPG

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on 12 Jul 2022, 09:50 last edited by ChrisW67 7 Dec 2022, 09:52
                  #8

                  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.

                  A 1 Reply Last reply 13 Jul 2022, 11:32
                  4
                  • C ChrisW67
                    12 Jul 2022, 09:50

                    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.

                    A Offline
                    A Offline
                    ankit thakar
                    wrote on 13 Jul 2022, 11:32 last edited by
                    #9

                    @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

                    1 Reply Last reply
                    0

                    1/9

                    11 Jul 2022, 09:30

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved