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.
  • S SamiV123

    @Christian-Ehrlicher said in error: undefined reference to `__imp_WSAStartup':

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

    the less you need to mess with CMake or (build files in general) the better.

    Using some non-standard stuff instead standarized scripting is for sure much better.
    If you would have read the post from above you would see that the OP is using MinGW which does not support this crap.

    Nothing standard about "scripting" (which I believe you use to refer to CMake) . But yeah, some of us prefer to have things the easy way and some don't...

    Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #20

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

    But yeah, some of us prefer to have things the easy way and some don't...

    Your idea will simply not work for the OP...

    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
    0
    • W wwwsharq

      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

      Ketan__Patel__0011K Offline
      Ketan__Patel__0011K Offline
      Ketan__Patel__0011
      wrote on 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")
      
      JonBJ 1 Reply Last reply
      0
      • Ketan__Patel__0011K Ketan__Patel__0011

        @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")
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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....

        Christian EhrlicherC 1 Reply Last reply
        0
        • JonBJ JonB

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

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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

          JonBJ 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

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

            They are not just an MSVC thing

            The pragma is proprietary MSVC crap

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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.

            Ketan__Patel__0011K 1 Reply Last reply
            0
            • S Offline
              S Offline
              SamiV123
              wrote on last edited by SamiV123
              #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
              • JonBJ JonB

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

                Ketan__Patel__0011K Offline
                Ketan__Patel__0011K Offline
                Ketan__Patel__0011
                wrote on 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
                
                JonBJ 1 Reply Last reply
                0
                • Ketan__Patel__0011K Ketan__Patel__0011

                  @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
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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).

                  Ketan__Patel__0011K 1 Reply Last reply
                  1
                  • JonBJ JonB

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

                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011
                    wrote on 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)
                    
                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • Ketan__Patel__0011K Ketan__Patel__0011

                      @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)
                      
                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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