消息中间件zeroMQ
1 czmq交叉编译
-
czmq的交叉编译移植参考此篇博客,比较详细
https://www.dazhuanlan.com/2019/12/10/5deee354ae256/ -
1.1 下载编译依赖库libzmq
git clone git://github.com/zeromq/libzmq.git
cd libzmq && ./autogen.sh
./configure --host=arm-linux --prefix=/home/xxx/arm-libzmq/ # 设置工具链和安装路径
make && make install
-
1.2 下载编译安装libcurl
下载地址:https://curl.haxx.se/download/
下载:
wget https://curl.haxx.se/download/curl-7.63.0.tar.gz
配置交叉编译环境
cd curl-7.63.0
./configure --host=arm-linux --prefix=/home/xxx/arm-libcurl/编译并安装
make && make install
-
1.3 libuuid交叉编译
下载源码
https://sourceforge.net/projects/libuuid/
配置交叉编译环境
cd libuuid-1.0.3
./configure --host=arm-linux --prefix=/home/xxx/arm-uuid/编译并安装
make && make install
-
1.4 安装libczmq库
下载源码git clone git://github.com/zeromq/czmq.git
生成环境
cd czmq && ./autogen.sh
配置交叉编译
./configure --host=arm-linux --prefix=/home/xxx/opt/arm-czmq/ CFLAGS="-I/home/xxx/arm-libzmq/include -I/home/xxx/arm- uuid/include" LDFLAGS="-L/home/xxx/arm-libzmq/lib -L/home/xxx/arm-uuid/lib -L/home/xxx/arm-libcurl/lib"
其中CFLAGS指定头文件路径, LDFLAGS指定库路径; 链接生成静态库时,有一些问题找不到libstdc++等库,但是动态库可以用。需要把动态库和libstdc++.so 相关库拷贝到开发板即可。编译并安装
make && make install
2 示例demo
2.1 example1
#include <stdio.h>
#include <stdlib.h>
#include <czmq.h>
int main (void)
{
// Create and bind server socket
zsock_t *server = zsock_new (ZMQ_PUSH);
zsock_bind (server, "tcp://*:9000");
// Create and connect client socket
zsock_t *client = zsock_new (ZMQ_PULL);
zsock_connect (client, "tcp://127.0.0.1:9000");
// Send a single message from server to client
zstr_send (server, "Hello");
char *message = zstr_recv (client);
assert (streq (message, "Hello"));
printf("recv:%s\n", message);
free (message);
puts ("Grasslands test OK");
zsock_destroy (&client);
zsock_destroy (&server);
return 0;
}