C ++:从标准库构建DLL时出现LNK2019错误

时间:2017-10-11 05:12:04

标签: c++ sockets dll lib mt4

我尝试构建一个DLL,允许我的MT4(一个着名的FX电子交易平台)也通过套接字与我的服务器通信(参见下面的代码,它在控制台程序中工作),使用Visual Studio 2017和遇到了LNK2019错误。

根据我从各种在线博客/论坛帖子的理解,这是由于依赖/从静态库等导出动态库,但不知道如何解决它。

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);
    }
}

错误讯息:

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   

0 个答案:

没有答案