1

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

The problem is, it doesn't matter what I do, I always get the following errors:

C:\Users\farin\AppData\Local\Temp\ccQCkFqw.o:main.cpp:(.text+0x37): undefined reference to `WSAStartup@8'
C:\Users\farin\AppData\Local\Temp\ccQCkFqw.o:main.cpp:(.text+0x48): undefined reference to `WSAGetLastError@0'
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 with:

/*
    Initialise Winsock
*/

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

#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;
    }
    
    printf("Initialised.");

    return 0;
}

I've already tried linking to the compiler, and possibly gone throught all the pages about the subject that I could find.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Rodriggrr
  • 11
  • 2

1 Answers1

0

Solution is here!

Just ran throught the command line, as follow:

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

Thanks to Retired Ninja.

Rodriggrr
  • 11
  • 2