Yesterday I've tried to make a socket server in C++, but I get errors upon compiling.
The errors:
Error 6 error LNK2019: unresolved external symbol _imp_socket#12 referenced in function "public: static unsigned long __cdecl Env::GetSocket(void)" (?GetSocket#Env##SAKXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 5 error LNK2019: unresolved external symbol _imp_listen#8 referenced in function "public: void __thiscall Network::Start(void)" (?Start#Network##QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 4 error LNK2019: unresolved external symbol _imp_htons#4 referenced in function "public: void __thiscall Network::Start(void)" (?Start#Network##QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 3 error LNK2019: unresolved external symbol _imp_bind#12 referenced in function "public: void __thiscall Network::Start(void)" (?Start#Network##QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 2 error LNK2001: unresolved external symbol "public: static class Network * Env::Network" (?Network#Env##2PAV0#A) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\HabboV5.obj HabboV5
Error 7 error LNK1120: 5 unresolved externals C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\Debug\HabboV5.exe HabboV5
My main .cpp class:
// HabboV5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "Env.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout.write("hi", 2);
cout << "Hello World!" << endl;
Env::Network = new Network();
Env::Network->Start();
while (1)
{
char input[256];
cin.getline(input, 256);
}
}
Network.h:
#pragma once
#include <WinSock2.h>
class Network
{
private:
SOCKET socket;
public:
Network(void);
void Start();
};
Network.cpp:
#include "StdAfx.h"
#include "Network.h"
#include <WinSock2.h>
#include "Env.h"
Network::Network(void)
{
}
void Network::Start()
{
this->socket = Env::GetSocket();
SOCKADDR_IN sInformation;
sInformation.sin_family = AF_INET;
sInformation.sin_addr.s_addr = INADDR_ANY;
sInformation.sin_port = htons(30000);
bind(this->socket, (SOCKADDR*) (&sInformation), sizeof(sInformation));
listen(this->socket, 10);
}
Env.h:
#include "stdafx.h"
#include "Network.h"
#include <WinSock2.h>
class Env
{
public:
static Network* Network;
static DWORD GetSocket()
{
return socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
};
In the linker options (on the project right-click, linker, input) you need add wsock32.lib or ws2_32.lib to the list of input files.
Related
I try to build a DLL to allow my MT4 ( a well-known FX e-trading platform ) to also communicate with my server via Sockets ( see the code below, it works in the console program ) using Visual Studio 2017 and have encountered LNK2019 error.
From what I understand from various online blog/forum posts, this is owing to dependencies / exporting a dynamic library from a static library etc., but have no idea how to fix it.
MT4.h
#pragma once
#ifdef MT4_EXPORTS
#define MT4_API __declspec(dllexport)
#else
#define MT4_API __declspec(dllimport)
#endif
namespace MT4
{
class Functions
{
public:
static MT4_API void main();
};
}
MT4.cpp
#pragma comment(lib, "Ws2_32.lib")
#include "stdafx.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include "MT4.h"
namespace MT4
{
void Functions::main()
{
char ipstr[] = "192.168.1.160";
int r;
WSAData wsaData;
WORD DLLVersion;
DLLVersion = MAKEWORD(2, 1);
r = WSAStartup(DLLVersion, &wsaData);
SOCKADDR_IN addr;
int addlen = sizeof(addr);
SOCKET sConnect;
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
in_addr tmp = { 0 };
InetPtonA(AF_INET, ipstr, &tmp);
addr.sin_addr = tmp;
addr.sin_family = AF_INET;
addr.sin_port = htons(9898);
connect(sConnect, (SOCKADDR*)&addr, sizeof(addr));
closesocket(sConnect);
}
}
Error Messages:
Severity Code Description Project File Line Suppression State
Error LNK1120 6 unresolved externals MT4 C:\Users\FutureC\source\repos\MT4\Debug\MT4.dll 1
Error LNK2019 unresolved external symbol __imp__closesocket#4 referenced in function "public: static void __cdecl MT4::Functions::main(void)" (?main#Functions#MT4##SAXXZ) MT4 C:\Users\FutureC\source\repos\MT4\MT4\MT4.obj 1
Error LNK2019 unresolved external symbol __imp__connect#12 referenced in function "public: static void __cdecl MT4::Functions::main(void)" (?main#Functions#MT4##SAXXZ) MT4 C:\Users\FutureC\source\repos\MT4\MT4\MT4.obj 1
Error LNK2019 unresolved external symbol __imp__htons#4 referenced in function "public: static void __cdecl MT4::Functions::main(void)" (?main#Functions#MT4##SAXXZ) MT4 C:\Users\FutureC\source\repos\MT4\MT4\MT4.obj 1
Error LNK2019 unresolved external symbol __imp__socket#12 referenced in function "public: static void __cdecl MT4::Functions::main(void)" (?main#Functions#MT4##SAXXZ) MT4 C:\Users\FutureC\source\repos\MT4\MT4\MT4.obj 1
Error LNK2019 unresolved external symbol __imp__WSAStartup#8 referenced in function "public: static void __cdecl MT4::Functions::main(void)" (?main#Functions#MT4##SAXXZ) MT4 C:\Users\FutureC\source\repos\MT4\MT4\MT4.obj 1
Error LNK2019 unresolved external symbol __imp__inet_pton#12 referenced in function "public: static void __cdecl MT4::Functions::main(void)" (?main#Functions#MT4##SAXXZ) MT4 C:\Users\FutureC\source\repos\MT4\MT4\MT4.obj 1
I've looked at other people's solutions to this problem and none of them, unfortunately, have solved my issue. I was having this error in my dev project, so I started from scratch, followed this tutorial to the letter, including the file names and locations, and I am still getting
1>unittest1.obj : error LNK2019: unresolved external symbol "public:
static int __cdecl HelloWorld::getTwo(void)"
(?getTwo#HelloWorld##SAHXZ) referenced in function "public: void
__thiscall UnitTest1::UnitTest1::TestMethodGetTwo(void)" (?TestMethodGetTwo#UnitTest1#1#QAEXXZ)
1>unittest1.obj : error
LNK2019: unresolved external symbol "public: int __thiscall
HelloWorld::getN(void)const " (?getN#HelloWorld##QBEHXZ) referenced in
function "public: void __thiscall
UnitTest1::UnitTest1::TestMethodGetN(void)"
(?TestMethodGetN#UnitTest1#1#QAEXXZ)
The code is in the tutorial I linked but I will copy it below. I don't understand what I could be doing wrong at this point - my test project depends on my build project, my definitions for these functions are in the class.
HelloWorld.h
#pragma once
class HelloWorld
{
int n = 10;
public:
static int getTwo();
int getN() const;
};
HelloWorld.cpp
#include "stdafx.h"
#include "HelloWorld.h"
int main()
{
return 0;
}
int HelloWorld::getTwo()
{
return 2;
}
int HelloWorld::getN() const
{
return n;
}
unittest1.cpp (in test project)
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../HelloWorld/HelloWorld.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethodGetTwo)
{
Assert::AreEqual(2, HelloWorld::getTwo());
}
TEST_METHOD(TestMethodGetN)
{
HelloWorld hw = HelloWorld();
Assert::AreNotEqual(0, hw.getN());
}
};
}
For some reason the linker can't find my definitions, and I'm out of ideas about why that might be.
When compiling my code I receive this error.
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Gabe Phelan\Documents\Visual Studio 2013\Projects\PA3 test\Debug\PA3 test.exe : fatal error LNK1120: 1 unresolved externals
#include <iostream>
#include <vector>
using namespace std;
class Heap{
private:
vector<int> heap;
int size;
public:
Heap(bool x);
};
#include "Header.h"
Heap::Heap(bool order){
int dummy = 0;
heap.push_back(dummy);
size = heap.size() - 1;
}
You have to have have an entry point, which is a function called main. So you need this,
int main(int argv, char* argc[])
{
//your code here
}
You dont declare classes in this, but what you want the program to execute.
i have central static lib in app that is windows simple executable.
now this center lib is just central point to using other helper library's im using
what i try to do is , to link statically all static library's into this static central lib
and then im my executable only link statically with this central lib .
for example :
i have this central static lib
#ifndef _HellperLib_H
#define _HellperLib_H
namespace HellperLib
{
class HellperLib
{
public:
HellperLib();
~HellperLib();
void invokeRequest();
private:
};
};
#endif
#include "HellperLib.h"
#include <stdio.h>
#include <curl/curl.h>
namespace HellperLib
{
HellperLib::HellperLib()
{
;
}
HellperLib::~HellperLib()
{
}
void HellperLib::invokeRequest()
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
}
}
as you can see its simple lib that is using curl static lib.
now i have another app that is executble tester
#include <stdio.h>
#include <stdlib.h>
#include "HellperLib.h"
int main(int argc, char **argv)
{
HellperLib::HellperLib hellperLib;
hellperLib.invokeRequest();
return 0;
}
to it i just add in the linker the hellperLib.lib file and path
but the linker yield me error:
1>HellperLib.lib(HellperLib.obj) : error LNK2019: unresolved external symbol __imp__curl_easy_strerror referenced in function "public: void __thiscall HellperLib::HellperLib::invokeRequest(void)" (?invokeRequest#HellperLib#1#QAEXXZ)
1>HellperLib.lib(HellperLib.obj) : error LNK2019: unresolved external symbol __imp__curl_easy_init referenced in function "public: void __thiscall HellperLib::HellperLib::invokeRequest(void)" (?invokeRequest#HellperLib#1#QAEXXZ)
1>HellperLib.lib(HellperLib.obj) : error LNK2019: unresolved external symbol __imp__curl_easy_setopt referenced in function "public: void __thiscall HellperLib::HellperLib::invokeRequest(void)" (?invokeRequest#HellperLib#1#QAEXXZ)
1>HellperLib.lib(HellperLib.obj) : error LNK2019: unresolved external symbol __imp__curl_easy_perform referenced in function "public: void __thiscall HellperLib::HellperLib::invokeRequest(void)" (?invokeRequest#HellperLib#1#QAEXXZ)
1>HellperLib.lib(HellperLib.obj) : error LNK2019: unresolved external symbol __imp__curl_easy_cleanup referenced in function "public: void __thiscall HellperLib::HellperLib::invokeRequest(void)" (?invokeRequest#HellperLib#1#QAEXXZ)
that means i need to add the curl static lib to the executable tester , and this i want to avoid if possible .
I have the following .h and .cpp files
If i have to I will include the full codes of the function definitions
When i compile my program i get the errors shown at the end
hash.h
#define BUCKETS 64
#define B_ENTRIES 50000
int curr_tanker;
typedef unsigned long int ulong;
typedef struct bucket
{
int bucket_id;
ulong bucket_entries;
}bucket;
typedef struct tanker_record
{
ulong tanker_id;
ulong tanker_size;
ulong num_of_entries;
ulong bucket_entry_count;
}tanker_record;
typedef struct fpinfo
{
unsigned long chunk_offset;
unsigned long chunk_length;
unsigned char fing_print[33];
}fpinfo;
struct fpinfo* InitHTable(fpinfo *);
int CreateTanker(tanker_record tr[]);
int Hash_CreateEntry(struct fpinfo *,struct fpinfo he,tanker_record tr);
ht.cpp
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
#include <iostream>
#include "ht.h"
struct fpinfo* InitHTable(struct fpinfo ht[][B_ENTRIES])
{
}
int CreateTanker(tanker_record tr[])
{
}
int
Hash_CreateEntry(struct fpinfo *t[][B_ENTRIES],struct fpinfo he,tanker_record tr[])
{
}
static void
WriteHTtoFile(struct fpinfo *t[][B_ENTRIES],int this_tanker)
{
}
main.cpp
#include<iostream>
#include"ht.cpp"
#include<conio.h>
#include<stdlib.h>
void main(int argc, char **argv)
{
static fpinfo hash_table[BUCKETS][B_ENTRIES];
static tanker_record tr[100];
InitHTable(&hash_table[0][0]);
CreateTanker(tr);
struct fpinfo fp;
...
ar = Hash_CreateEntry(&hash_table[0][0], fp,tr[0]);
i get the following errors when i try to compile it using vc2010
1>main.obj : error LNK2005: "struct fpinfo * __cdecl InitHTable(struct fpinfo (* const)[50000])" (?InitHTable##YAPAUfpinfo##QAY0MDFA#U1##Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl CreateTanker(struct tanker_record * const)"
(?CreateTanker##YAHQAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl Hash_CreateEntry(struct fpinfo * (* const)[50000],struct fpinfo,struct tanker_record * const)" (?Hash_CreateEntry##YAHQAY0MDFA#PAUfpinfo##U1#QAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int curr_tanker" (?curr_tanker##3HA) already defined in ht.obj
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl Hash_CreateEntry(struct fpinfo *,struct fpinfo,struct tanker_record)"
(?Hash_CreateEntry##YAHPAUfpinfo##U1#Utanker_record###Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "struct fpinfo * __cdecl InitHTable(struct fpinfo *)" (?InitHTable##YAPAUfpinfo##PAU1##Z) referenced in function _main
THANKS FOR YOUR HELP!!
You're including ht.cpp from main.cpp, which will include all the definitions of functions already defined in ht.cpp itself.
You want to include ht.h instead.
It won't help in this situation, but you should also protect the header file with include guards:
#ifndef HT_H
#define HT_H
// contents of ht.h
#endif
You also need the arguments of the function declarations to match those of the definitions:
struct fpinfo* InitHTable(struct fpinfo[][B_ENTRIES]);
// Missing: ^^^^^^^^^^^
int CreateTanker(tanker_record tr[]); // OK
int Hash_CreateEntry(struct fpinfo*[][B_ENTRIES],struct fpinfo,tanker_record[]);
// Missing ^^^^^^^^^^^^^ ^^
Add an "include guard" in your header, so that it its contents aren't "seen" twice after preprocessing. For Microsoft, #pragma once at the beginning of the .h file. In general, add:
#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif
Make sure to adopt a consistent "unique" naming scheme for each of your headers. __YOUR_HEADER_H would do, for example customio.h into __CUSTOM_IO_H.