0

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   
user3666197
  • 1
  • 6
  • 50
  • 92
Ben KWAN
  • 1
  • 1
  • Can you post the full error message? – Asesh Oct 11 '17 at 05:14
  • Hi Asesh, Thanks for your prompt reply. I have edited the post with the error messages. – Ben KWAN Oct 11 '17 at 05:18
  • 1
    Please provide a [mcve]. If it is a link error, the command line argument is part of the problem – Passer By Oct 11 '17 at 05:18
  • Hi Passer By, understood, those are the problems that I suspect but not sure and seems better to present the problem as a whole. Sorry that I missed error messages in the very first bit. Being new to the community and I will bear in mind. Thanks. – Ben KWAN Oct 11 '17 at 05:24
  • 2
    Looks like VS doesn't pick your #pragma for linking with winsock library. Have you tried including this into project settings? "Project->Properties->Configuration Properties->Linker->Input->Additional dependencies" then go to the end and type ";ws2_32.lib". – vasek Oct 11 '17 at 05:37
  • Hi Vasek, all great! Thanks for your help!!! How can I vote for you? – Ben KWAN Oct 11 '17 at 06:26

0 Answers0