0

I'm Trying to build a simple socket.

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

int getaddrinfo(const char *node,
                const char *service,
                const struct addrinfo *hints,
                struct addrinfo **res);

int main(int argc, char *argv[])
{
    WSADATA wsa;
    SOCKET a;
    printf("Initializing...");
    if(WSAStartup(MAKEWORD(2,2),&wsa)!=0);
        printf("Failed, Error:%d",WSAGetLastError());
    if((a=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET);
        printf("Failed, Error:",WSAGetLastError());
    return 0;
}

But getting the error "Undefined reference to..." on all winsock2 related functions(like WSAStartup).

dbush
  • 205,898
  • 23
  • 218
  • 273

2 Answers2

0

You need to link with ws2_32.lib to get the winsock functions.

If you're using a project file, from within VS go to Configuration Properties -> Linker -> Input and add ws2_32.lib to Additional Dependencies.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

Undefined reference errors mean you have not linked the socket library to your project. The header files tell it what the functions look like but the library files actually do the work. See for this answer for details of how to do it.

Mike
  • 2,721
  • 1
  • 15
  • 20