-2

i'm new to network programming and i am trying out a tutorial on . here's the program i tried.

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


using namespace std;

int main()
{
    WSADATA wsaData;
    int iResult;
    //initialize  winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if(iResult!=0)
    {
        cout << "Startup Failed:"<<iResult<< endl;
    }

    return 1;
}

the error i got is:

D:\CodeBlocks\MinGW\bin\Network\main.cpp|14|undefined reference to `WSAStartup@8'|

again, this is my first time doing network programming and i donno why this error is coming. I think i should install windows sdk. someone do tell me if i should and what i should install.

dirkk
  • 6,160
  • 5
  • 33
  • 51

1 Answers1

0

As Alex Farber mentioned you have to link against Ws2_32.lib in order for the linker to find the actual code for WSAStartup. He knows ( from the winsock2 header ) how it is declared, but not his actual implementation, that is found in the associated library file, that can either be static ( .lib ) or dynamic ( dlls on windows ).

Before asking this kind of question you should try googling the error yourself and you'll find tons of people that had your same problem.

A more general advice : When you encounter this kind of problems, if a first research with the 'exact' same error message doesn't work ( highly doubt, but it can happen ) try researching a more general error 'undefined reference to' and you'll easily satisfy your thirst for knowledge ( :D ).

If you want a quick overview of compilation / linking see How does the compilation/linking process work?. Hope this answers it and gives you a few hints on how to search for help ^^

Have a nice day and happy coding :)

Community
  • 1
  • 1