user3102962
user3102962

Reputation: 366

C++ dll file using 0mq

I'm trying to write dll C++ library. I use 0mq to create sockets. I have 2 files in my library: Library.cpp & Library.h In Library.h I wrote this

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

But when I try to use theese sockets in Library.cpp I can't because they are only visible in class ExchangeConnection constructor

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

And other functions of ExchangeConnection class can't see theese initialized sockets

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

when I try to build Library, I get errors:

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

How can I handle this?

Upvotes: 1

Views: 515

Answers (1)

im so confused
im so confused

Reputation: 2111

making this an answer only for formatting purposes. your cpp is wrong:

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

should be

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

note the lack of new as i explained the differences in new from C# to C++, and you didn't put the initialization at file scope like I said, but left it in the constructor.

Upvotes: 1

Related Questions