一、编译安装

1.1、安装依赖

sudo apt-get install libtool pkg-config build-essential autoconf automake

1.2、编译安装ZMQ使用的加密库

git clone git://github.com/jedisct1/libsodium.git
cd libsodium
./autogen.sh
./configure
make check
sudo make install
sudo ldconfig
cd ../

1.3、编译安装libzmq

git clone git://github.com/zeromq/libzmq.git
cd libzmq
./autogen.sh
./configure –with-libsodium
make
sudo make install
sudo ldconfig
cd ../

1.4、安装ZMQ的c库

        添加编译选项 ​​-lczmq -lzmq。​

git clone git://github.com/zeromq/czmq.git
cd czmq
./autogen.sh
./configure && make check
sudo make install
sudo ldconfig
cd -

1.5、添加ZMQ的C++库

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh # bootstrap-vcpkg.bat for Powershell
./vcpkg integrate install
./vcpkg install cppzmq
cd -

二、使用

2.1、cmake使用

        cmake下使用,需要再CMakeList文件中添加如下内容:

#find cppzmq wrapper, installed by make of cppzmq
find_package(cppzmq)
target_link_libraries(*Your Project Name* cppzmq)

2.2、实例

        C++开发可以参考如下实例代码进行开发工作:

        server端:

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

        Client端:

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