silent
silent

Reputation: 2904

starting out in winsock cant get the first bit to compile

apologies for having to create a new thread for this but I'm starting out in winsock and have been following through madwizard and beej guides, and also some information on msdn. I'm stuck and I cant seem to compile the following (yeahp...big start..i know..:P )

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <iostream>

using namespace std;

int main(void){
    const int iReqWinsockVer = 2; // 
    WSADATA wsadata;

    if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsadata) == 0 ){
        if (LOBYTE(wsadata.wVersion) >= iReqWinsockVer){


        }
        else{
            cout<<" Required version Not available..." << endl;
        }
        if (WSACleanup()!=0){
            cout<<"Clean up failed!..." << endl;
        }
    }
    else{
        cout<<"Startup faled!..." << endl;
    }

    return 0;

}

I'm receiving the following errors

C:\DOCUME~1\rs\LOCALS~1\Temp/ccygafwl.o:sock.cpp:(.text+0x14a): undefined reference to `WSAStartup@8'
C:\DOCUME~1\rs\LOCALS~1\Temp/ccygafwl.o:sock.cpp:(.text+0x186): undefined reference to `WSACleanup@0'
collect2: ld returned 1 exit status

I'm using GCC and compiling through the command-linem is there anything that I must link?.

-Thank you.

Upvotes: 1

Views: 374

Answers (3)

icecrime
icecrime

Reputation: 76745

You have to link your project against the winsock library using -lws2_32 (if I remember correctly).

Upvotes: 1

ROAR
ROAR

Reputation: 1324

gcc -o socket.exe socket.c -lws2_32

Upvotes: 1

Sanja Melnichuk
Sanja Melnichuk

Reputation: 3505

Hi i think you didnt link with with win socket lib (Ws2_32.lib in vissual studio)

Upvotes: 0

Related Questions