1.安装
C库 libzmq
sudo apt install libzmq3-dev
实例: https://zeromq.org/get-started/?language=c\&library=libzmq#
编译依赖:
pkg-config --cflags --libs libzmq
or cat /usr/lib/x86_64-linux-gnu/pkgconfig/libzmq.pc
-isystem /usr/include/mit-krb5 -I/usr/include/pgm-5.2 -lzmq
只使用ZMQ基本功能, 添加链接选项 -lzmq 即可
编译选项: 无
链接选项: -lzmqpp
C++库 libzmqpp
sudo apt install libzmqpp-dev
实例: https://zeromq.org/get-started/?language=cpp\&library=zmqpp#
编译依赖
- 不支持.pc 格式查看
- 通过安装位置, 可知头文件,动态库位置
/usr/include/zmqpp/zmqpp.hpp
/usr/lib/x86_64-linux-gnu/libzmqpp.so
编译选项: 无
链接选项: -lzmqpp
2.实例
1.代码
1_zmqpp_sub订阅端.cc
c++
#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;
}
2_zmqpp_pub发布端.cc
c++
#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;
}
2.scons构建
SConstruct
python
## 模板2
env = Environment()
env["PROGSUFFIX"] = ".out" # 可执行后缀.out
env["CCFLAGS"] = " -g3 -O0 -Wall" # gdb 调试开关
env["LIBS"]=["zmqpp","zmq","protobuf"]
env.Program("1_zmqpp_sub订阅端.cc")
env.Program("2_zmqpp_pub发布端.cc")
scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o 1_zmqpp_sub订阅端.o -c -ggdb3 -O0 -Wall 1_zmqpp_sub订阅端.cc
g++ -o 1_zmqpp_sub订阅端.out 1_zmqpp_sub订阅端.o -lzmqpp -lzmq -lprotobuf
g++ -o 2_zmqpp_pub发布端.o -c -ggdb3 -O0 -Wall 2_zmqpp_pub发布端.cc
g++ -o 2_zmqpp_pub发布端.out 2_zmqpp_pub发布端.o -lzmqpp -lzmq -lprotobuf
scons: done building targets.
3.运行
终端1: ./1_zmqpp_sub订阅端.out
终端2: ./2_zmqpp_pub发布端.out
效果如下图
1.安装
C库 libzmq
sudo apt install libzmq3-dev
实例: https://zeromq.org/get-started/?language=c\&library=libzmq#
编译依赖:
pkg-config --cflags --libs libzmq
or cat /usr/lib/x86_64-linux-gnu/pkgconfig/libzmq.pc
-isystem /usr/include/mit-krb5 -I/usr/include/pgm-5.2 -lzmq
只使用ZMQ基本功能, 添加链接选项 -lzmq 即可
编译选项: 无
链接选项: -lzmqpp
C++库 libzmqpp
sudo apt install libzmqpp-dev
实例: https://zeromq.org/get-started/?language=cpp\&library=zmqpp#
编译依赖
- 不支持.pc 格式查看
- 通过安装位置, 可知头文件,动态库位置
/usr/include/zmqpp/zmqpp.hpp
/usr/lib/x86_64-linux-gnu/libzmqpp.so
编译选项: 无
链接选项: -lzmqpp
2.实例
1.代码
1_zmqpp_sub订阅端.cc
c++
#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;
}
2_zmqpp_pub发布端.cc
c++
#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;
}
2.scons构建
SConstruct
python
## 模板2
env = Environment()
env["PROGSUFFIX"] = ".out" # 可执行后缀.out
env["CCFLAGS"] = " -g3 -O0 -Wall" # gdb 调试开关
env["LIBS"]=["zmqpp","zmq","protobuf"]
env.Program("1_zmqpp_sub订阅端.cc")
env.Program("2_zmqpp_pub发布端.cc")
scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o 1_zmqpp_sub订阅端.o -c -ggdb3 -O0 -Wall 1_zmqpp_sub订阅端.cc
g++ -o 1_zmqpp_sub订阅端.out 1_zmqpp_sub订阅端.o -lzmqpp -lzmq -lprotobuf
g++ -o 2_zmqpp_pub发布端.o -c -ggdb3 -O0 -Wall 2_zmqpp_pub发布端.cc
g++ -o 2_zmqpp_pub发布端.out 2_zmqpp_pub发布端.o -lzmqpp -lzmq -lprotobuf
scons: done building targets.
3.运行
终端1: ./1_zmqpp_sub订阅端.out
终端2: ./2_zmqpp_pub发布端.out
效果如下图
![[Pasted image 20240423180836.png]]
3.其它用法实例
git clone下载代码, 切换到对应的工作目录, 安装对应的库之后,运行scons即可编译, 运行
zmq传输结构体实例
7_zmq_server_发送接受_结构体.c
8_zmq_client_发送接受_结构体.c
zmq传输protobuf 序列化实例
3_zmqpp_server.cc message.pb.cc
4_zmqpp_client.cc message.pb.cc
c++库zmqpp实例
11_zmqpp_请求_应答_server.cc
12_zmqpp_请求_应答_client.cc
13_zmqpp_推送_拉取_pusher.cc
14_zmqpp_推送_拉取_puller.cc
15_zmqpp_进场间通信_sender.cc
16_zmqpp_进场间通信_receiver.cc