Can't add "ws2_32.lib" in any way

Asked 2 days ago
Viewed 6 times

I'm beggining with socket programming in C++, and as a normal student, i went to search online.

The problem is, don't matter what I do I always get the following error codes:

C:\Users\farin\AppData\Local\Temp\ccQCkFqw.o:main.cpp:(.text+0x37): undefined reference to `[email protected]'
C:\Users\farin\AppData\Local\Temp\ccQCkFqw.o:main.cpp:(.text+0x48): undefined reference to `[email protected]'
collect2.exe: error: ld returned 1 exit status
I'm using MinGW in Visual Studio Code.
Here's the sample code i've been messing around:
    Initialise Winsock


#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
    WSADATA wsa;
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;

    return 0;

I've already tried linking to the compiler and possibly gone throught all the pages about the subject that i could find. So Stack Overflow, you're my last shot.

asked 2 days ago

Correct Answer

Solution is here!

Just ran throught the command line, as follow:

g++  main.cpp -o main.exe -lws2_32 

Thanks to Retired Ninja.

answered 2 days ago