zeromq / cppzmq

Header-only C++ binding for libzmq
http://www.zeromq.org
MIT License
1.94k stars 757 forks source link

value_type': is not a member of 'std::iterator_traits<ForwardIter> in zmq.hpp (version 4.6.0 from vcpkg) #441

Closed ghost closed 4 years ago

ghost commented 4 years ago

Trying to compile Visual Studio 2019 into a .dll, containing cppzmq and libzmq.

Build succeeds with only libzmq, with code like:

    int major = 0;
    int minor = 0;
    int patch = 0;
    zmq_version(&major, &minor, &patch);
    std::wcout << "Current 0MQ version is " << major << '.' << minor << '.' << patch << '\n';

Output: Current 0MQ version is 4.3.3

Installed libzmq and cppzmq like this:


1: Install vcpkg

2: Run:
vcpkg install zeromq:x64-windows
vcpkg install zeromq:x86-windows
vcpkg install cppzmq

3: Copy files:

From:
(your vcpk location)\vcpkg-2020.07\packages\zeromq_x86-windows\bin\libzmq-mt-4_3_3.dll
To:
(git)\sources\zeromq\libx86\libzmq-mt-4_3_3.dll

From:
(your vcpk location)\vcpkg-2020.07\packages\zeromq_x86-windows\lib\libzmq-mt-4_3_3.lib
To:
(git)\sources\zeromq\libx86\libzmq-mt-4_3_3.lib

From:
(your vcpk location)\vcpkg-2020.07\packages\zeromq_x64-windows\bin\libzmq-mt-4_3_3.dll
To:
(git)\sources\zeromq\libx64\libzmq-mt-4_3_3.dll

From:
(your vcpk location)\vcpkg-2020.07\packages\zeromq_x64-windows\lib\libzmq-mt-4_3_3.lib
To:
(git)\sources\zeromq\libx64\libzmq-mt-4_3_3.lib

All files from:
(your vcpk location)\vcpkg-2020.07\packages\zeromq_x64-windows\include\*
To:
(git)\sources\zeromq\include\*

All files from:
(your vcpk location)\vcpkg-2020.07\packages\cppzmq_x86-windows\include\*
To:
(git)\sources\zeromq\include\*

Building by just including the headers works, but including some code does not:

Code added:

    zmq::context_t context(1);
    zmq::socket_t socket(context, ZMQ_REP);
    socket.bind("tcp://localhost:5555");

    std::array<zmq::message_t, 4> do_step_msg = {
        zmq::message_t(REQUEST_fmi2DoStep, 2),
        zmq::message_t(static_cast<char*>(static_cast<void*>(&t)),1),
        zmq::message_t(static_cast<char*>(static_cast<void*>(&tNext)),1),
        zmq::message_t(0) };

Error during build:


'value_type': is not a member of 'std::iterator_traits<ForwardIter>'    sources\zeromq\include\zmq.hpp  334 
missing type specifier - int assumed. Note: C++ does not support default-int    sources\zeromq\include\zmq.hpp  334 
syntax error: missing ';' before identifier 'value_t'   sources\zeromq\include\zmq.hpp  334 
'value_t': undeclared identifier    sources\zeromq\include\zmq.hpp  334 
'std::distance': no matching overloaded function found  sources\zeromq\include\zmq.hpp  338 
Failed to specialize function template 'iterator_traits<_Iter>::difference_type std::distance(_InIt,_InIt)' sources\zeromq\include\zmq.hpp  338 
'value_t': undeclared identifier    sources\zeromq\include\zmq.hpp  338 
'size_': an object of const-qualified type must be initialized  sources\zeromq\include\zmq.hpp  337 
'value_t': undeclared identifier    sources\zeromq\include\zmq.hpp  342 
'zmq::message_t::data': no matching overloaded function found   sources\zeromq\include\zmq.hpp  342 
'zmq::message_t::data': invalid template argument for 'T', type expected    sources\zeromq\include\zmq.hpp  342 
'std::copy': no matching overloaded function found  sources\zeromq\include\zmq.hpp  342 
'_OutIt std::copy(_InIt,_InIt,_OutIt)': expects 3 arguments - 2 provided    \sources\zeromq\include\zmq.hpp 342 

I'm not sure why libzmq shows 4.3.3 when running my app, but cppzmq states version 4.6.0 in vcpkg - I assume they are developed at entirely different paces and that 4.6.0 of cppzmq should be compatible with 4.3.3 of libzmq. Am I missing something obvious? I undertand all I need is the headers and a working libzmq, since "cppzmq is a light-weight, header-only binding. You only need to include the header file zmq.hpp (and maybe zmq_addon.hpp) to use it."

Any help is greatly appreciated!

sigiesec commented 4 years ago

Re versioning: The zmq_version is that of libzmq. Since it can be a dynamic library, it's useful to query this at compile time. The version of cppzmq is only made available via compile-time macros, as it is a header-only library. The versions do not correspond.

Re the syntax error: Unfortunately, the compiler messages are not complete, so it's not easy to tell where they come from. When using Visual Studio, it's better to copy the compiler output than the error list. But I suspect that zmq::message_t(0) is the problem. I am not sure what this ought to mean? But it's also unclear what type REQUEST_fmi2DoStep has.

ghost commented 4 years ago

I was able to compile by code by commenting out the following block in zmq.hpp:

    /*
    template<class ForwardIter> message_t(ForwardIter first, ForwardIter last)
    {
        typedef typename std::iterator_traits<ForwardIter>::value_type value_t;

        assert(std::distance(first, last) >= 0);
        size_t const size_ =
          static_cast<size_t>(std::distance(first, last)) * sizeof(value_t);
        int const rc = zmq_msg_init_size(&msg, size_);
        if (rc != 0)
            throw error_t();
        std::copy(first, last, data<value_t>());
    }
    */

My code also compiles if I remove the "zmq::message_t(0)", for some reason I found some example code and just tried to use that as-is. Thank you for your fast reply!