LNK2019: unauthorized external character

I saw many other similar questions, but I just could not understand this problem with their help. I realized that this is a binding problem, but from what I see, I have a straightened connection.

I am writing a chat server / client (using in this article ).

I defined a class for storing server functions and a header file that handles all incoming messages.

This is the header file:

#include <windows.h> #include <winsock.h> #include <stdio.h> #include <tchar.h> #include <strsafe.h> #include "resource1.h" class ChatServer { public: int InitServer(HINSTANCE hInst); public: void ReportError(int errorCode, const char *whichFunc); }; 

This is the actual server class:

 #include "server.h" #define NETWORK_ERROR -1 #define NETWORK_OK 0 //Keeps stuff for the server int ChatServer::InitServer(HINSTANCE hInst) { WORD sockVersion; WSADATA wsaData; int nret; sockVersion = MAKEWORD(1,1); //Version 1.1 //Init winsock WSAStartup(sockVersion, &wsaData); //Create listening socket SOCKET listeningSocket; //AFINET - Go over TCP //SOCK_STREAM - Stream oriented socket //IPPROTO_TCP - Use tcp rather than udp listeningSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP); if(listeningSocket == INVALID_SOCKET) { nret = WSAGetLastError(); //Get error detail ReportError(nret, "socket()"); WSACleanup(); return NETWORK_ERROR; } SOCKADDR_IN serverInfo; serverInfo.sin_family = AF_INET; serverInfo.sin_addr.s_addr = INADDR_ANY; serverInfo.sin_port = htons(1337); //Bind the socket to local server address. nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); if(nret == SOCKET_ERROR) { nret = WSAGetLastError(); ReportError(nret, "bind()"); WSACleanup(); return NETWORK_ERROR; } //Make socket listen nret = listen(listeningSocket, 10); //Up to 10 connections at the same time. if(nret = SOCKET_ERROR) { nret = WSAGetLastError(); ReportError(nret, "listen()"); WSACleanup(); return NETWORK_ERROR; } //Wait for client SOCKET theClient; theClient = accept(listeningSocket, NULL, NULL); if(theClient == INVALID_SOCKET) { nret = WSAGetLastError(); ReportError(nret, "accept()"); WSACleanup(); return NETWORK_ERROR; } //Send and receive from the client, and finally, closesocket(theClient); closesocket(listeningSocket); //shutdown WSACleanup(); return NETWORK_OK; } void ChatServer::ReportError(int errorCode, const char *whichFunc) { char errorMsg[92]; // Declare a buffer to hold // the generated error message ZeroMemory(errorMsg, 92); // Automatically NULL-terminate the string // The following line copies the phrase, whichFunc string, and integer errorCode into the buffer sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode); MessageBox(NULL, errorMsg, "socketIndication", MB_OK); } 

Finally, the main.cpp file with the entry method for the program calls "ChatServer :: InitServer (g_hInst)". It is quite large, so I omitted it, but if necessary, I will publish it as well.

The error messages I get are similar to the ones below, but all of them pose problems with the api functions associated with the winsockets API:

 Error 3 error LNK2019: unresolved external symbol _closesocket@4 referenced in function "public: int __thiscall ChatServer::InitServer(struct HINSTANCE__ *)" ( ?InitServer@ChatServer @@ QAEHPAUHINSTANCE__@ @@Z) 

As I said before, I believe this problem is related to the compiler, not understanding what to do with functions like "closesocket" that should be associated with winsock.h.

Thanks for any advice and thanks for reading all this gibberish :)

+4
source share
3 answers

These linker errors are always the same: you are using a symbol that the linker cannot find. You need to tell the linker the link to the libraries containing these characters.

As I said, I believe that this problem has something to do with the compiler does not understand what to do with functions such as "closesocket", which should be associated with winsock.h.

No, closesocket not related to winsock.h. winsock.h is a header file, not a library. It may contain a closesocket and this is normal for the compiler, but the linker really needs to know where the code for this function is so that it can link your program with it.

You are likely to use winsock2.h instead of winsock.h. Windows Sockets API 2.0 dates from 1994, and version 1 is deprecated.

You can see below this function documentation that the library in which it is located is ws2_32.lib.

+8
source

Your class can be defined as follows:

 class ChatServer { public: int InitServer(HINSTANCE hInst); void ReportError(int errorCode, const char *whichFunc); }; 

However, you are mistaken for not including the library entry. Add a dependency on the winsock library in the project input line, also consider using winsock2.h.

For reference: http://msdn.microsoft.com/en-us/library/ms737629(v=vs.85).aspx

+2
source

Just including the .h file does not call the actual code that implements the functions that should be connected. The .h file tells the compiler that the functions exist somewhere, and what their signature is. You still need to create a library where the function is actually implemented for the linker.

+2
source

Source: https://habr.com/ru/post/1369514/


All Articles