ubuntu+QT环境下搭建zmq库

(1)编译前准备

  • 需要先安装libtoolautoconf、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.csdn.net/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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值