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