(1)编译前准备
需要先安装libtool、autoconf、git、boost
sudo apt install libtool
sudo apt install autoconf
sudo apt-get install git
sudo apt-get update
sudo apt-get install libboost-all-dev
(2)按照gitup上的步骤安装zeromq/zmqpp
注意问题:
(a). libsodium 可以不安装;
(b). 安装过程可能提示权限不够,处理方法:切换到root用户(sudo su root)再执行
(c). 错误提示,可暂时不处理
*** 1 failure is detected in the test module "zmqpp"
make: *** [Makefile:179:installcheck] 错误 201
(d). git clone 默认下载最新的tag,目前libzmq搭建的版本通过程序获取到的版本是4.3.5,但gitup上最新的版本是4.3.4,原因暂时未知
(e). git clone 可能会遇到下载失败等问题,可以尝试修改命令如下(具体参考博文:https://blog.youkuaiyun.com/wesoner/article/details/129421851)
git clone https://github.com/zeromq/zmqpp.git
(f). 执行./autogen.sh可能出现错误,解法:改成执行./autogen.sh -f -s
步骤如下:
# Build, check, and install libsodium
git clone git://github.com/jedisct1/libsodium.git
cd libsodium
./autogen.sh
./configure && make check
sudo make install
sudo ldconfig
cd ../
# Build, check, and install the latest version of ZeroMQ
git clone git://github.com/zeromq/libzmq.git
cd libzmq
./autogen.sh
./configure --with-libsodium && make
sudo make install
sudo ldconfig
cd ../
# Now install ZMQPP
git clone git://github.com/zeromq/zmqpp.git
cd zmqpp
make
make check
sudo make install
make installcheck
(3)QT配置
在编译之前,需要在QT工程文件加上下面这行,才可以正确链接到zmqpp库,cmake同理。
LIBS += /usr/local/lib/libzmqpp.a -lzmq
INCLUDEPATH += /usr/local/include
(4)测试
跑个简单的sub-pub模式
sub端:
#include <iostream>
#include <string>
#include <zmqpp/zmqpp.hpp>
using namespace std;
int main (int argc, char *argv [])
{
zmqpp::context context_;
const string addr_port = "tcp://localhost:5555";
zmqpp::socket_type type = zmqpp::socket_type::subscribe;
zmqpp::socket socket_ = zmqpp::socket(context_,type);
socket_.set(zmqpp::socket_option::subscribe, "");
socket_.connect(addr_port);
while(1) {
zmqpp::message message;
socket_.receive(message);
std::cout <<"recv data: "<< message.get(0)<< std::endl;
}
return 0;
}
pub端:
#include <iostream>
#include <string>
#include <zmqpp/zmqpp.hpp>
#include <unistd.h>
using namespace std;
int main ()
{
zmqpp::context context_;
const string addr_port = "tcp://*:5555";
zmqpp::socket_type type = zmqpp::socket_type::publish;
zmqpp::socket socket_ = zmqpp::socket(context_,type);
socket_.bind(addr_port);
int i=0 ;
while(i<100) {
zmqpp::message message;
message << "test[" + to_string(i++)+"]";
socket_.send(message);
sleep(1);
}
return 0;
}