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.
  • ankit thakarA Offline
    ankit thakarA Offline
    ankit thakar
    wrote on 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] ?

    jsulmJ 1 Reply Last reply
    0
    • ankit thakarA ankit thakar

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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

      ankit thakarA 1 Reply Last reply
      0
      • jsulmJ jsulm

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

        ankit thakarA Offline
        ankit thakarA Offline
        ankit thakar
        wrote on last edited by ankit thakar
        #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 ?

        jsulmJ 1 Reply Last reply
        0
        • ankit thakarA ankit thakar

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          • ankit thakarA Offline
            ankit thakarA Offline
            ankit thakar
            wrote on 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;
            }
            
            
            jsulmJ 1 Reply Last reply
            0
            • ankit thakarA ankit thakar

              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;
              }
              
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              • ankit thakarA Offline
                ankit thakarA Offline
                ankit thakar
                wrote on last edited by
                #7

                Capture.JPG

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by ChrisW67
                  #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.

                  ankit thakarA 1 Reply Last reply
                  4
                  • C ChrisW67

                    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.

                    ankit thakarA Offline
                    ankit thakarA Offline
                    ankit thakar
                    wrote on 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

                    • Login

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