1

I am using Mac, and i use homebrew to install zeromq. I want to use zeromq for my application. I tried to compile zmq.hpp https://github.com/zeromq/cppzmq/blob/master/zmq.hpp with

int main () {
    //  Prepare our context and socket
    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_REP);
    socket.bind ("tcp://*:5555");

    while (true) {
        zmq::message_t request;

        //  Wait for next request from client
        socket.recv (&request);
        std::cout << "Received Hello" << std::endl;

        //  Do some 'work'
        sleep (1);

        //  Send reply back to client
        zmq::message_t reply (5);
        memcpy ((void *) reply.data (), "World", 5);
        socket.send (reply);
    }
    return 0;
}

but it failed. The error shows

"frameworks/zmq/zmq.hpp:377:35: error: expected ';' at end of declaration list socket_t (const socket_t&) ZMQ_DELETED_FUNCTION; ^ ; frameworks/zmq/zmq.hpp:379:42: error: expected ';' at end of declaration list void operator = (const socket_t&) ZMQ_DELETED_FUNCTION;"

Why is this happened? The zmq.hpp code does not contain any errors. Please help.

Finally I do this and it worked.

#if __has_feature(cxx_deleted_functions)
        #define ZMQ_DELETED_FUNCTION = delete
    #else
        #define ZMQ_DELETED_FUNCTION
    #endif

Thanks a lot. Audrey.

nan
  • 1,131
  • 4
  • 18
  • 35

1 Answers1

6

The macro ZMQ_DELETED_FUNCTION was apparently introduced to provide conditional support for such C++11 feature as "deleted functions" (= delete). Your compiler does not seem to support that C++11 feature. Hence the error.

By design, the zmq.hpp attempts to set this macro automatically, by analyzing the compiler version and defining the macro accordingly. It is possible that this automatic detection is being too optimistic. However, it is also possible that your compiler actually supports that feature, you just forgot to turn it on in the compiler settings.

BTW, I'm looking at the Clang section of the code that defines the macro

  #elif defined(__clang__)
    #if __has_feature(cxx_rvalue_references)
        #define ZMQ_HAS_RVALUE_REFS
    #endif

    #if __has_feature(cxx_deleted_functions)
        #define ZMQ_DELETED_FUNCTION = delete
    #endif

and it looks broken to me. If the compiler does not support cxx_deleted_functions feature, then macro ZMQ_DELETED_FUNCTION remains undefined (instead of being defined as empty). This is wrong.

Are you using Clang? If so, this could actually be the reason for your error. In that case the error can be fixed by pre-defining ZMQ_DELETED_FUNCTION as an empty macro, either as a global macro definition or in the source code before including zmq.hpp.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • is there a way to resolve it, if I don't have the latest compiler? – nan Jan 30 '13 at 17:34
  • @user1510529: What compiler are you using specifically? GCC? Clang? – AnT stands with Russia Jan 30 '13 at 17:36
  • I am using gcc 4.2.1. ((GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)) – nan Jan 30 '13 at 17:41
  • @user1510529: If you are using GCC, you should try specifying `-std=c++11` or `-std=c++0x` or `-std=gnu++11` or `-std=gnu++0x` in the command line, depending on your intent and on the version of GCC. – AnT stands with Russia Jan 30 '13 at 17:44
  • 1
    You'll need somewhere near 4.7 for `= delete;`, though. – chris Jan 30 '13 at 17:49
  • @chris: The "compiler detection" code seems to assume that 4.2 is sufficient as long as "c++0x experimental features" are enabled. If that is not the case, then it is quite possible that someone simply mixed `2` and `7`. Happens all the time in handwriting... – AnT stands with Russia Jan 30 '13 at 17:55
  • @AndreyT, Ah, I only ever used `-std=c++0x` and `-std=c++11`, so I didn't see them added until around then. – chris Jan 30 '13 at 17:56
  • @AndreyT Actually I am using makefile, I add a line in makefile DEFINES=-std=c++11, but it reports "cc1plus: error: unrecognized command line option "-std=c++11" and "Undefined symbols for architecture x86_64: "_main", referenced from: __start in gcrt1.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)" I don't know why?? – nan Jan 30 '13 at 17:58
  • @user1510529: Your compiler is apparently too old to know what `-std=c++11` is. Try using `-std=c++0x` instead. – AnT stands with Russia Jan 30 '13 at 18:00
  • well, it again reports error "cc1plus: error: unrecognized command line option "-std=c++0x"" Very strange. Before I didn't specify any thing and there were no such errors. – nan Jan 30 '13 at 18:09
  • @AndreyT What should I specify if the compiler is clang? I don't know what is the default compiler on a Mac. It seems that I have both on my Mac – nan Jan 30 '13 at 18:12
  • @user1510529: Well, did you try to compile the code with Clang as is? What did it say? Errors as well? – AnT stands with Russia Jan 30 '13 at 18:42
  • @AndreyT Yes, errors as well. I tried several settings such as -std-c++0x -std-c++11, all reported the same error. Finally I just define ZMQ_DELETED_FUNCTION as empty and it compiled without erros. it worked now. Thanks a lot~ – nan Jan 31 '13 at 10:45