Reference error in winsock2

0

I am trying to create a socket in c with windows api winsock2.h but when trying to compile my code it returns me the following error

  

undefined referrence to WSAStartup

     

undefined referrence to WSAGetLast

     

Error: ld returned 1 exit status

winsock boot code

#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;
}
    
asked by anonymous 18.09.2017 / 00:40

1 answer

1

1) Remove this line of code

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

2) In the IDE CodeBlocks, insert a new project, I have named Sockets

3)ChooseConsoleApplicationandclickGO

Inthewindowthatappears,clickNext.andthenchoosetheClanguage

4)Inthemain.cfunctionputyourcodewithoutusing#pragma

#include<stdio.h>#include<winsock2.h>intmain(intargc,char*argv[]){WSADATAwsa;printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.");

    return 0;
}

5) Right-click on the project name and choose Build Options

6)Choose(Click)intheLinkerSettingstabandinLinkLibrariesclicktheADDbutton

7)Typethefollowing:ws2_32andclickOK

8)Compileandseethatthereisnoerrornow

9)Whenrunningthemessageappearsinagoodonenow!Goodstudy!!! OBs: If in another IDE see how to insert the library because the GCC compiler often does not recognize or consider bilbioteca inclusions via directive #pragma

    
18.09.2017 / 19:40