Search code examples
c++lnk2019

lnk2019 visual studio error


I am trying to make a simple UDP socket class to do some talking between a c++ program and a java program so I'm trying to make a socket class that handles all the UDP transmission but I can't get the program to compile because I get about 8 lnk2019 errors and I have no idea what they even mean. I mainly work in Java and only use c++ when I have to lol. I have 3 files the socket header and code. Also the Udp Socket code is from rFactor-Nesim so the socket code isn't written by me.

UdpSocket.cpp

#include "UdpSocket.hpp"

#include <stdio.h>

UdpSocket::UdpSocket(const char* host, int port) 
: mHost(host), mPort(port)
{
}

UdpSocket::~UdpSocket(void)
{
}

void UdpSocket::Open()
{
if(WSAStartup(MAKEWORD(2, 0), &mWinsockData) != 0)
    fprintf(stderr, "WSAStartup() failed");   

if ((mSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    perror("socket() failed");   

memset(&mSocketAddress, 0, sizeof(mSocketAddress));
mSocketAddress.sin_family = AF_INET;
mSocketAddress.sin_addr.s_addr = inet_addr(mHost);
mSocketAddress.sin_port = htons(mPort);
}

void UdpSocket::Close()
{
closesocket(mSocket);  
WSACleanup();
}

void UdpSocket::Send(char* str, size_t length)
{
size_t result = sendto(mSocket, str, length, 0, 
    (struct sockaddr *) &mSocketAddress, sizeof(mSocketAddress));

if(result != length)
    perror("sendto() sent incorrect number of bytes");   
}

UdpSocket.hpp

#ifndef UDPSOCKET_HPP
#define UDPSOCKET_HPP

#include <WinSock.h>

class UdpSocket
{
public:
UdpSocket(const char* host, int port);
~UdpSocket(void);

void Send(char* str, size_t length);

void Open();
void Close();
private:
const char* mHost;
int mPort;
int mSocket;
struct sockaddr_in mSocketAddress;
WSADATA mWinsockData;
};

#endif // UDPSOCKET_HPP

and the main

#include "Socket/UdpSocket.hpp"
#include <iostream>


int Main(){
UdpSocket* testSocket = new UdpSocket("127.0.0.1", 27469);
testSocket->Open();
system("pause");
return 0;
}

Any help would be great. I'm not very strong with c++ but I have done a little bit

Console Ouput:

Error   1   error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open@UdpSocket@@QAEXXZ)   UdpSocket.obj   SocketTest
Error   2   error LNK2019: unresolved external symbol __imp__inet_addr@4 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open@UdpSocket@@QAEXXZ)   UdpSocket.obj   SocketTest
Error   3   error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open@UdpSocket@@QAEXXZ) UdpSocket.obj   SocketTest
Error   4   error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open@UdpSocket@@QAEXXZ)  UdpSocket.obj   SocketTest
Error   5   error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function "public: void __thiscall UdpSocket::Close(void)" (?Close@UdpSocket@@QAEXXZ)    UdpSocket.obj   SocketTest
Error   6   error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function "public: void __thiscall UdpSocket::Close(void)" (?Close@UdpSocket@@QAEXXZ)   UdpSocket.obj   SocketTest
Error   7   error LNK2019: unresolved external symbol __imp__sendto@24 referenced in function "public: void __thiscall UdpSocket::Send(char *,unsigned int)" (?Send@UdpSocket@@QAEXPADI@Z)  UdpSocket.obj   SocketTest
Error   8   error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup   MSVCRTD.lib SocketTest
Error   9   fatal error LNK1120: 8 unresolved externals C:\Users\Brendan\Documents\Visual Studio 2008\Projects\SocketTest\Debug\SocketTest.exe  SocketTest

Solution

  • It sounds like you're failing to link against Winsock - Ws2_32.lib

    If you're building from the command line, add Ws2_32.lib to your link command line.

    If you're building from Visual Studio, look for linker flags/settings inside a project configuration dialog.