使用0mq的C ++ dll文件

时间:2013-12-14 19:50:44

标签: c++ sockets dll zeromq

我正在尝试编写dll C ++库。我使用0mq来创建套接字。 我的库中有2个文件: Library.cpp& Library.h 在Library.h中我写了这个

namespace Exchange
{
    class ExchangeConnection
    {

        static zmq::socket_t *sub, *req;
        static zmq::message_t message;
        ExchangeConnection();

static __declspec(dllexport) 

        long long SendPlaceMessage
            (char* user_id,

     std::tuple<long long, long long> price, long long quantity,
 bool buy, char* asset1, char* asset2, long long &error);
    }

但是当我尝试在Library.cpp中使用theese套接字时我不能,因为它们只在类ExchangeConnection构造函数中可见

ExchangeConnection::ExchangeConnection(){
    zmq::socket_t sub(context, ZMQ_SUB);
    sub.bind("tcp://127.0.0.1:1000");
    sub.setsockopt(ZMQ_SUBSCRIBE, "", 0);
    zmq::socket_t req(context, ZMQ_REQ);
    req.bind("tcp://127.0.0.1:1001");
    }

ExchangeConnection类的其他功能无法看到theese初始化套接字

long long ExchangeConnection::SendCancelMessage
(long long order_id, char *user_id, long long &error)

当我尝试构建库时,我收到错误:

Error   1   error LNK2001: unresolved external symbol "private: static class zmq::socket_t * Exchange::ExchangeConnection::sub" (?sub@ExchangeConnection@Exchange@@0PAVsocket_t@zmq@@A) C:\Users\LibraryDll\LibraryDll.obj  LibraryDll
Error   2   error LNK2001: unresolved external symbol "private: static class `zmq::socket_t * Exchange::ExchangeConnection::req" (?req@ExchangeConnection@Exchange@@0PAVsocket_t@zmq@@A)    C:\Users\LibraryDll\LibraryDll.obj  LibraryDll`
Error   3   error LNK2001: unresolved external symbol "private: static class zmq::message_t Exchange::ExchangeConnection::message" (?message@ExchangeConnection@Exchange@@0Vmessage_t@zmq@@A)   C:\Users\LibraryDll\LibraryDll.obj  LibraryDll

我该如何处理?

1 个答案:

答案 0 :(得分:1)

仅为格式化目的而使其成为答案。你的cpp错了:

namespace Exchange{ 
    ExchangeConnection::ExchangeConnection() { 
        zmq::context_t ExchangeConnection::context = new zmq::context_t(1); 
        zmq::socket_t ExchangeConnection::sub = new zmq::socket_t(context, ZMQ_SUB);
        ...
    }
    ...
}

应该是

namespace Exchange{ 
    zmq::context_t ExchangeConnection::context = zmq::context_t(1);
    zmq::socket_t ExchangeConnection::sub = zmq::socket_t(context, ZMQ_SUB);
    ExchangeConnection::ExchangeConnection() {
        ...
    }
    ...
}

注意缺少new因为我解释了new从C#到C ++的差异,并且你没有像我说的那样将初始化放在文件范围内,而是将它留在构造函数中。