0

I use Windows and codeblocks 13.12 IDE. I'm writing a C program need functions in <Winsock2.h> (like "WSAStartup" function) ; because Logs showed "Undefined Reference to WSAStartup", I included the command below according to the article WSAStartup link error.

#include <stdio.h>
#include <Winsock2.h>
#pragma comment(lib,"ws2_32.lib")

in case compiling process went wrong while linking library, I had put my program in a project and added "ws2_32.lib" into list of "Link libraries"; however, Logs only shows "cannot find -lws2_32.lib" I'm confused because I have checked that "ws2_32.dll" file exists in my C:\Windows\System32. How should I resolve the problem?

Ainz
  • 5
  • 5
  • `#pragma comment(lib,"ws2_32.lib")` only works with Microsoft's compiler. If you are using GCC or clang it won't work. It looks like it is GCC or clang because adding the library to the "Link libraries" creates a command-line switch of `-l` (the Microsoft compiler uses a / for its command-line parameters) To get rid of the undefined reference error you actually need ws2_32.lib - the ws2_32.dll is used only when running and not when compiling. Do you have ws2_32.lib somewhere? – Jerry Jeremiah May 31 '21 at 05:58
  • 2
    I think for Link Libraries you should use "ws2_32" because the ".lib" extension is only for Microsoft link libraries. The command-line option created should have been "-lws2_32" which look for the GCC/clang version of the link library. "-lws2_32" would look for "libws2_32.a" - do you have libws2_32.a somewhere? – Jerry Jeremiah May 31 '21 at 06:04
  • Related: https://stackoverflow.com/questions/63001406/cannot-find-lws2-32-lib-error-in-codeblocks-on-windows – Jerry Jeremiah May 31 '21 at 06:06
  • I am not familiar with Codeblocks, but for GCC, you need to specify option `-lws2_32`. That should be enough, but if the library is not in a standard location, you can specify a library search path with the `-L` option. In the IDE you should be able to find a setting called something like "compiler/linker options" to specify these options. – nielsen May 31 '21 at 08:34
  • @JerryJeremiah thank you, because you said "ws2_32.lib" is not the link libraries for GCC, I had been trying to add "ws2_32" 、"-lws2_32"、and "ws2_32.a" into the link libraries list, and "ws2_32" works to find the file (maybe it is the file "libws2_32.a" , I'm not sure but it works). thank you for your clear explain! – Ainz May 31 '21 at 13:04

0 Answers0