19

I am using Code::Blocks, MinGW, and Windows. Im trying to initialize the winsock so that I can work on a project. I keep getting the error Undefined Reference to WSAStartup@8 Anyone know how to go about fixing this?

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>

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

int main(int argc , char *argv[]){
    WSADATA wsa;
    int output;

    output=WSAStartup(MAKEWORD(2,2),&wsa);
    if(output != 0) {
        printf("Startup failed %d\n", output);
        return 1;
    } else {
        printf("Initialized");
        return 0;
    }

}
MD XF
  • 7,860
  • 7
  • 40
  • 71
Paulo
  • 191
  • 1
  • 1
  • 4

5 Answers5

42

Linker looks for dependencies after the code was loaded. If library appeared in the building process before the symbols were needed, because source files appeared after that, then no symbols were used and later when they appear in source files they will be unresolved. Place the winsock library -lws2_32 that you link with AFTER the source and object files.

gcc prog.c -o prog -lws2_32
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • Excuse me, but where could the source and objects files be in Qt Creator? To be able to link this library as I have the same issue but another environment – Ivan Silkin Oct 08 '19 at 22:30
  • They are not _in_ QT Creator, but rather referenced by the project. If you click on the file in QT Creator list of sources you will find the phisical location. – 4pie0 Oct 12 '19 at 21:10
3

You may should check your compiler options, add -lws2_32 to add linker options when linking. I use TDM-GCC, works well after that.

3

2019 update for those using Codeblocks on windows:

First, on the menubar, click settings, then compiler, then switch to the tab that says "linker settings". From here, click add, open the file explorer, go to C:/ directory, and type "ws2_32" into the search bar. One file should show up: "libws2_32.a". Add this file by clicking open, then ok, (the file should now appear in the box on the left) and then ok again. Now remove the #PRAGMA line from your code and try to compile, and things should work just fine.

Jay
  • 1,289
  • 3
  • 11
  • 22
2

I made another way, I find the library that contain funtion that compiler can't link to, then I add to linker of compiler. and almost of librarys are in the lib folder of MINGW (often be C:/MinGW/lib); like thisThese are libraries I add when I got some errors with Dlib Or you can do this instruction for auto regconite missing lib. Building a wxWidgets program in Code::Blocks

Community
  • 1
  • 1
1

Your source code shows that you use the very specific, (to Microsoft's compiler), #pragma comment(lib,"ws2_32.lib") statement. There are two problems with this:

  1. This pragma isn't valid in GCC, (i.e. MinGW compilers), so it is simply ignored, both at compile and link time.
  2. In MinGW, (in common with GCC convention on most (maybe all?) other platforms), there is no "ws2_32.lib"; the correct name for the library, (which is an import library for ws2_32.dll), is "libws2_32.a".

To resolve your issue, you must not rely on MSVC specific pragmas, in your source code; rather, you must specify the library correctly on the linking command line, (almost) as tinky_winky shows:

gcc prog.c -o prog.exe [...other .c and .o files...] -lws2_32 ...

(and ensure that any libraries you specify come after the object files, or source files, which require them).

Keith Marshall
  • 1,980
  • 15
  • 19