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. error: undefined reference to `__imp_WSAStartup'
Forum Updated to NodeBB v4.3 + New Features

error: undefined reference to `__imp_WSAStartup'

Scheduled Pinned Locked Moved Unsolved General and Desktop
winsockwindows10qt 6.6
29 Posts 7 Posters 7.6k Views 1 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.
  • W wwwsharq
    26 Mar 2024, 13:03

    i am not sure why this doesn't quite work in qt, i am trying to use the windows sdk
    and for some reason i get this error

    #include <sstream>
    #include <filesystem>

    #ifdef _WIN32
    #include "winsock2.h"
    #include <windows.h>
    #endif

    #include <QScrollBar>
    #include <QSettings>
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    //there's a bunch of other includes that don't matter for this i think btw

      #include "ws2tcpip.h"
     #include "stdio.h"
     using namespace std;
     using namespace stn;
     #pragma comment(lib, "Ws2_32.lib")
    
    
    
    
      MainWindow::MainWindow(QWidget *parent)
       : QMainWindow(parent)
       , ui(new Ui::MainWindow)
       , dev(this)
    {
      ui->setupUi(this);
    
    setFixedSize(size());           // prevent resizing
    
    setControlsState();
    
    ui->pushButtonSave->setEnabled(false);
    
    timerId = startTimer(1000);
    
    // Asynchronous initialization. Continued asynchronously in MainWindow::event, then in asyncInit.
    // QEvent must be allocated on the heap, released by receiver.
    QCoreApplication::postEvent(
        this,                                   // receiver
        new QEvent(QEvent::User));}
     MainWindow::~MainWindow()
    {
       delete ui;
    
    killTimer(timerId);}
    const ApplicationSettings* MainWindow::getSettings() const
    {
    return &settings;
    }
    

    // Asynchronous initialization.
    // Constructor - event - asyncInit.
    void MainWindow::asyncInit()
    {
    #ifdef _WIN32
    // Windows: Program working with SIXTeen SDK is responsible to initialize Windows sockets.

    WSADATA wsa_data;
    
    if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
    {
        postLogString("WSAStartup failed");
        ui->pushButtonConnect->setEnabled(false);
    }
    

    #endif

    //thank you in advance

    K Offline
    K Offline
    Ketan__Patel__0011
    wrote on 4 Apr 2024, 14:15 last edited by
    #21

    @wwwsharq for this error there are so many resone and i personally faced many problems
    You should try to rearrange your header files.

    Try the following way which is i used.

    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    #include <NetCon.h>
    
    #pragma comment (lib, "Ws2_32.lib")
    #pragma comment (lib, "Mswsock.lib")
    #pragma comment (lib, "AdvApi32.lib")
    
    J 1 Reply Last reply 4 Apr 2024, 14:25
    0
    • K Ketan__Patel__0011
      4 Apr 2024, 14:15

      @wwwsharq for this error there are so many resone and i personally faced many problems
      You should try to rearrange your header files.

      Try the following way which is i used.

      #define WIN32_LEAN_AND_MEAN
      #include <windows.h>
      #include <WinSock2.h>
      #include <WS2tcpip.h>
      #include <NetCon.h>
      
      #pragma comment (lib, "Ws2_32.lib")
      #pragma comment (lib, "Mswsock.lib")
      #pragma comment (lib, "AdvApi32.lib")
      
      J Offline
      J Offline
      JonB
      wrote on 4 Apr 2024, 14:25 last edited by JonB 4 Apr 2024, 14:28
      #22

      @Ketan__Patel__0011
      Have you verified these #pragma comment (lib, ... statements work from MinGW? They are not just an MSVC thing?

      Maybe your re-ordering of the headers will help, I don't know, but I don't see how this will solve MinGW not knowing to link with Ws2_32, which seems to be the error message.

      Same sort of thing as @SamiV123 above. Don't forget the OP actually already has

       #pragma comment(lib, "Ws2_32.lib")
      

      in his original code question, and it's still not linking....

      C 1 Reply Last reply 4 Apr 2024, 14:29
      0
      • J JonB
        4 Apr 2024, 14:25

        @Ketan__Patel__0011
        Have you verified these #pragma comment (lib, ... statements work from MinGW? They are not just an MSVC thing?

        Maybe your re-ordering of the headers will help, I don't know, but I don't see how this will solve MinGW not knowing to link with Ws2_32, which seems to be the error message.

        Same sort of thing as @SamiV123 above. Don't forget the OP actually already has

         #pragma comment(lib, "Ws2_32.lib")
        

        in his original code question, and it's still not linking....

        C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 4 Apr 2024, 14:29 last edited by
        #23

        @JonB said in error: undefined reference to `__imp_WSAStartup':

        They are not just an MSVC thing

        The pragma is proprietary MSVC crap

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        J 1 Reply Last reply 4 Apr 2024, 14:41
        0
        • C Christian Ehrlicher
          4 Apr 2024, 14:29

          @JonB said in error: undefined reference to `__imp_WSAStartup':

          They are not just an MSVC thing

          The pragma is proprietary MSVC crap

          J Offline
          J Offline
          JonB
          wrote on 4 Apr 2024, 14:41 last edited by
          #24

          @Christian-Ehrlicher I know. I was inviting @Ketan__Patel__0011 to consider his answer for the OP with MinGW in this light.

          K 1 Reply Last reply 5 Apr 2024, 06:33
          0
          • S Offline
            S Offline
            SamiV123
            wrote on 4 Apr 2024, 16:17 last edited by SamiV123 4 Apr 2024, 16:31
            #25

            Using MingW on Windows when MSVS is free (and is essentially the de-facto standard C++ tool for Windows) is just bonkers.

            1 Reply Last reply
            0
            • J JonB
              4 Apr 2024, 14:41

              @Christian-Ehrlicher I know. I was inviting @Ketan__Patel__0011 to consider his answer for the OP with MinGW in this light.

              K Offline
              K Offline
              Ketan__Patel__0011
              wrote on 5 Apr 2024, 06:33 last edited by
              #26

              @JonB Well i am using this conecpt in MSVC based Application and i also tried with MinGW Application and it's working very well.
              But For MinGW you have to do one more thing.
              just add below code in your .pro file

              LIBS += -lWs2_32
              LIBS += -lMswsock
              LIBS += -lAdvApi32
              
              J 1 Reply Last reply 5 Apr 2024, 06:48
              0
              • K Ketan__Patel__0011
                5 Apr 2024, 06:33

                @JonB Well i am using this conecpt in MSVC based Application and i also tried with MinGW Application and it's working very well.
                But For MinGW you have to do one more thing.
                just add below code in your .pro file

                LIBS += -lWs2_32
                LIBS += -lMswsock
                LIBS += -lAdvApi32
                
                J Offline
                J Offline
                JonB
                wrote on 5 Apr 2024, 06:48 last edited by
                #27

                @Ketan__Patel__0011
                So for MinGW the #pragma comment (lib, "Ws2_32.lib") has no effect, that is why you have to add the -lWs2_32 etc. explicitly to get the link to happen. Which is just what we said --- the #pragma is for MSVC only.

                If you read what the OP is asking you will see that are using cmake, not qmake, no .pro file. So your LIBS lines do not help them. And they are asking precisely what equivalent they have to do for cmake (which I do not know).

                K 1 Reply Last reply 5 Apr 2024, 07:31
                1
                • J JonB
                  5 Apr 2024, 06:48

                  @Ketan__Patel__0011
                  So for MinGW the #pragma comment (lib, "Ws2_32.lib") has no effect, that is why you have to add the -lWs2_32 etc. explicitly to get the link to happen. Which is just what we said --- the #pragma is for MSVC only.

                  If you read what the OP is asking you will see that are using cmake, not qmake, no .pro file. So your LIBS lines do not help them. And they are asking precisely what equivalent they have to do for cmake (which I do not know).

                  K Offline
                  K Offline
                  Ketan__Patel__0011
                  wrote on 5 Apr 2024, 07:31 last edited by
                  #28

                  @JonB
                  So For CMake You can try the below code.

                  target_link_libraries(${CMAKE_PROJECT_NAME} Ws2_32)
                  target_link_libraries(${CMAKE_PROJECT_NAME} Mswsock)
                  target_link_libraries(${CMAKE_PROJECT_NAME} AdvApi32)
                  
                  C 1 Reply Last reply 5 Apr 2024, 07:43
                  0
                  • K Ketan__Patel__0011
                    5 Apr 2024, 07:31

                    @JonB
                    So For CMake You can try the below code.

                    target_link_libraries(${CMAKE_PROJECT_NAME} Ws2_32)
                    target_link_libraries(${CMAKE_PROJECT_NAME} Mswsock)
                    target_link_libraries(${CMAKE_PROJECT_NAME} AdvApi32)
                    
                    C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 5 Apr 2024, 07:43 last edited by
                    #29

                    @Ketan__Patel__0011 What are you discussing here? The op already used this three days ago...

                    target_link_libraries(eccoci PRIVATE ws2_32)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1

                    21/29

                    4 Apr 2024, 14:15

                    • Login

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