Mingw Linker Error: Winsock

MinGW linker error: winsock

Put the -lws2_32 AFTER the list of object files - GCC searches libraries and object files in the order they appear on the command line.

Just to help the other viewers out there:

gcc hello.c -o hello.o -lws2_32

C++ Linker error LNK2001 and LNK1120 Winsock2 Lib

The error is not a compiler error, it is a linker error. There is a difference between the two, and knowing the difference will make you more aware of the issue at hand.

The linker error states that it cannot find the Socket::Socket(void) function implemented. Looking at your code, this seems that the implementation of the no-argument (default) constructor for Socket is missing.

Your code compiled without issues, since the compiler doesn't care whether the function(s) you're calling really do exist. As long as there is a declaration of the function (just as you have in your code), or the function body is located before the call of the function, the compiler says "ok, no problem".

After successful compilation, the linker now actually tries to look for the functions that your code is calling. If it can't find the function, either in the other object modules or libraries, then the linker gives you the "unresolved external" error. If you get those errors, read the error message carefully, as the error is stating what function(s) is (are) being called, but cannot be found.

That gobbledy-gook that looks like the linker is going nuts -- that is just name mangling and not the concern for this type of error. The important part of the message is the class and function name, along with the parameters (which in this case is void, meaning no arguments).

C winsock2.h WS2_32.lib linking undefined refernce to

First of all remove #pragma comment(lib, "WS2_32.lib") this is only Visual C++ Compiler directive.

Second one, you are not provided CMakeLists.txt, did you know CLion aims on cross platform solutions and configures make files for your specified environment (at toolchain tab) from CMake. That means you don't need to call gcc and link libraries manually, all you need is target_link_libraries(myexecutable ws2_32) after add_executable(myexecutable).

Third, you don't need wsock32.lib actually linked to your project, unless you want backward compatibility with Win95 (I'm sure you don't). For further details look into this answer.

Your top-level CMake file should looks like:

# system
cmake_minimum_required(VERSION 3.6)
set(CMAKE_C_STANDARD 99)

# project
project(WinsockExample C)

# sources
set(source_files
main.c
)

# build
add_executable(${CMAKE_PROJECT_NAME} ${source_files})
target_link_libraries(${CMAKE_PROJECT_NAME} ws2_32)

Winsock server code not compiling with MinGW

I was able to fix the problem bt adding

#define _WIN32_WINNT 0x501


Related Topics



Leave a reply



Submit