Visual Studio Error lnk2019

I am trying to make a simple UDP socket class to talk between a C ++ program and a java program, so I am trying to create a socket class that handles all UDP transmission, but I cannot force the program to compile because I get about 8 lnk2019 errors, and I have no idea what they mean. I mainly work in Java and only use C ++ when I need lol. I have 3 files socket header and code. Also the Udp Socket code is from rFactor-Nesim, so the socket code is not 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 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 am not very strong with C ++, but I did a little work

Ouput Console:

 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 
+4
source share
2 answers

It looks like you are not Ws2_32.lib Winsock - Ws2_32.lib

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

If you are creating Visual Studio, find the linker flags / settings in the project configuration dialog.

+6
source

when you have code in several implementation files, you need to compile all these implementation files and transfer the resulting object code files to the linker, which combines them (and other things) into an executable

this is not enough to include the module title

C ++ does not yet have the concept of a technical module, so including headers does not magically compile implementation files or pass object code files to the linker

this is not part of the C ++ standard, but it is part of the everyday use of tools

The linker tells you that you did not provide it with object code for functions of your class Winsock libraries

this object code is provided by a library file, in Visual C ++, usually with a .lib file name extension "


in general, when you get a mysterious error, just find the error number in the documentation

in a visual studio that is as simple as pressing F1

0
source

All Articles