cppzmq is a C++ binding for libzmq. It has the following design goals:
There are other C++ bindings for ZeroMQ with different design goals. In particular, none of the following bindings are header-only:
These examples require at least C++11.
#include <zmq.hpp> int main() { zmq::context_t ctx; zmq::socket_t sock(ctx, zmq::socket_type::push); sock.bind("inproc://test"); sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait); }
This a more complex example where we send and receive multi-part messages over TCP with a wildcard port.
#include <iostream> #include <zmq_addon.hpp> int main() { zmq::context_t ctx; zmq::socket_t sock1(ctx, zmq::socket_type::push); zmq::socket_t sock2(ctx, zmq::socket_type::pull); sock1.bind("tcp://127.0.0.1:*"); const std::string last_endpoint = sock1.get(zmq::sockopt::last_endpoint); std::cout << "Connecting to " << last_endpoint << std::endl; sock2.connect(last_endpoint); std::array<zmq::const_buffer, 2> send_msgs = { zmq::str_buffer("foo"), zmq::str_buffer("bar!") }; if (!zmq::send_multipart(sock1, send_msgs)) return 1; std::vector<zmq::message_t> recv_msgs; const auto ret = zmq::recv_multipart( sock2, std::back_inserter(recv_msgs)); if (!ret) return 1; std::cout << "Got " << *ret << " messages" << std::endl; return 0; }
See the examples directory for more examples. When the project is compiled with tests enabled, each example gets compiled to an executable.
For an extensive overview of the zmq.hpp API in use, see this Tour of CPPZMQ by @brettviren.
Bindings for libzmq in zmq.hpp:
Types:
zmq::context_tzmq::ctxoptzmq::socket_tzmq::socket_refzmq::socket_typezmq::sockoptzmq::send_flagszmq::recv_flagszmq::message_tzmq::const_bufferzmq::mutable_bufferzmq::recv_buffer_sizezmq::send_result_tzmq::recv_result_tzmq::recv_buffer_result_tzmq::error_tzmq::monitor_tzmq_event_t,zmq::free_fn,zmq::pollitem_t,zmq::fd_tzmq::poller_t DRAFTzmq::event_flags DRAFTzmq::poller_event DRAFTFunctions:
zmq::versionzmq::pollzmq::proxyzmq::proxy_steerablezmq::bufferzmq::str_bufferExtra high-level types and functions zmq_addon.hpp:
Types:
zmq::multipart_tzmq::active_poller_t DRAFTFunctions:
zmq::recv_multipartzmq::send_multipartzmq::send_multipart_nzmq::encodezmq::decodeThe users of cppzmq are expected to follow the guidelines below to ensure not to break when upgrading cppzmq to newer versions (non-exhaustive list):
The following macros may be used by consumers of cppzmq: CPPZMQ_VERSION, CPPZMQ_VERSION_MAJOR, CPPZMQ_VERSION_MINOR, CPPZMQ_VERSION_PATCH.
The contribution policy is at: http://rfc.zeromq.org/spec:22
Build steps:
Build libzmq via cmake. This does an out of source build and installs the build files
Build cppzmq via cmake. This does an out of source build and installs the build files
Build cppzmq via vcpkg. This does an out of source build and installs the build files
Using this:
A cmake find package scripts is provided for you to easily include this library. Add these lines in your CMakeLists.txt to include the headers and library files of cpp zmq (which will also include libzmq for you).
#find cppzmq wrapper, installed by make of cppzmq
find_package(cppzmq)
target_link_libraries(*Your Project Name* cppzmq)