Why am I getting undefined reference errors and how do I link in Visual Studio?

Viewed 58

The error messages I am receiving are as follows:

C:\Users\Chris\AppData\Local\Temp\ccQAKGQO.o:Lab7Server.c:(.text+0x49): undefined reference to `WSAStartup@8'
C:\Users\Chris\AppData\Local\Temp\ccQAKGQO.o:Lab7Server.c:(.text+0xd6): undefined reference to `getaddrinfo'
C:\Users\Chris\AppData\Local\Temp\ccQAKGQO.o:Lab7Server.c:(.text+0xf7): undefined reference to `WSACleanup@0'

As far as I understand, this is because I am not linking to the library "Ws2_32" when compiling Lab7Server.c. However, after attempting to understand how to link using gcc (-L, -o, etc) I am walking myself in circles and still getting the error. I understand there is a way in Visual Studio to link the correct library, but I also cannot find the option to do that. I have attempted to look at the properties of the file, however, that was just entirely blank. What am I doing wrong here?

#define WIN32_LEAN_AND_MEAN

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

#pragma comment(lib, "Ws2_32.lib");

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int main(void){
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL;
    struct addrinfo hints;
    int iResult;
    int recvbufflen = DEFAULT_BUFLEN;
    
    // Initialize WinSock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    // Resolve the server address and port
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }

}

NOTE: Code is incredibly incomplete but I would like to know how to link libraries before I move on.

0 Answers
Related