美文网首页
ZeroMQ交叉编译到ARM设备上使用

ZeroMQ交叉编译到ARM设备上使用

作者: 望江樵夫 | 来源:发表于2022-01-18 18:19 被阅读0次

【ZeroMQ官方指南】
Libzmq - the low level library
本文以最新发布版本为例(截止2022.1.18版本号zeromq-4.3.4)
下载传送门:zeromq-4.3.4.tar.gz
本文移植的目标平台是ARM的cortex-A9,交叉编译工具使用的是:gcc-linaro-arm-linux-gnueabihf-4.8-2014.04_linux.tar.xz
注:交叉编译工具链可根据自己的平台更改

文件及环境准备

下载zeromq-4.3.4.tar.gz后解压到当前文件夹,打开终端,进入该文件夹:

mhming@xz:~/Desktop/ZeroMQ/sources/zeromq-4.3.4$ 

下载交叉编译工具链解压后根据自己的工具链路径设置环境变量,例如本文:

export PATH=$PATH:/home/mhming/Desktop/ZeroMQ/gcc4.8/bin

编译配置

./configure --host=arm-linux-gnueabihf --prefix=/home/mhming/Desktop/ZeroMQ/sources/arm-zeromq/ --without-libsodium

说明:--host配置ARM目标主机类型(简单点就是交叉编译工具链的前缀),--prefix配置安装路径(我们需要的文件都放在这里)


环境及编译配置

编译安装

make && make install
编译完成

环境移植

本文安装目录:/home/mhming/Desktop/ZeroMQ/sources/arm-zeromq/
将安装目录下的include文件夹和lib文件夹内的同文件及库文件拷贝到ARM环境下的/usr/include和/usr/lib目录即可使用

      主机安装目录                   ARM环境目录
arm-zeromq/ include/*   ---->    /usr/include
arm-zeromq/ lib/*       ---->    /usr/lib

使用测试

主机端按装pyzmq:

pip3 install pyzmq

主机端安装zmq:

sudo apt-get install libczmq-dev

上述安装主机环境为ubuntu20.04,其他系统环境请参考官方文档。

C++版本server源码:

注意:正常最小化编译按转的头文件中不包含zmq.hpp头文件,可以将ubuntu20.04安装文件中的zmq.hpp(一般在/usr/include目录下)拷贝至ARM环境

//server.cpp
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>

typedef struct{
    uint8_t error;
    uint8_t part;
    uint8_t action;
    uint8_t index;
    float times;
}set_action_t;//注意四字节对齐

int main () {
    // Prepare our context and socket
    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_REP);
    socket.bind ("tcp://*:5555");
    set_action_t actions;

    while (true) {
        zmq::message_t request;

        // Wait for next request from client
        socket.recv (&request);
        memcpy(&actions.part,request.data(),request.size()); 

        printf("size:%5d,%5d,%5d,%5d,%5d,%5f\n",request.size(),actions.error, actions.part,actions.action,actions.index,actions.times);

        // Do some 'work'
        sleep (1);

        // Send reply back to client
        zmq::message_t reply (5);
        memcpy ((void *) reply.data (), "World", 5);
        socket.send (reply);
    }
    return 0;
}
C++版本cilent源码:
//client.cpp
#include <zmq.hpp>
#include <string>
#include <iostream>

int main () {
    uint8_t data[7] = {0};
    uint8_t idx = 0;
    float *fp = (float*)(data+3);
    data[idx++] = 5;
    data[idx++] = 12;
    data[idx++] = 123;
    *fp = 456.78;
    
    // initialize the zmq context with a single IO thread
    zmq::context_t context(1);
    // construct a REQ (request) socket and connect to interface
    zmq::socket_t socket(context, ZMQ_REQ);
    socket.connect("tcp://192.168.50.111:5555");

    for(idx = 0; idx < 5; idx++)
    {
        // send the request message
        printf("Sending %d ...\n",idx);
        socket.send(data, 0);
        
        // wait for reply from server
        zmq::message_t reply;
        socket.recv(&reply);
    printf("Received reply %d [ ",idx);
    std::cout << reply.to_string() << " ]" <<std::endl;
    }
    return 0;
}
python版本client源码:
#client.py
#coding=utf-8
import struct

import zmq

context = zmq.Context()
print('connect to hello world server')
socket =  context.socket(zmq.REQ)
socket.connect('tcp://localhost:5555')

part = 5
action = 12
index = 123
times = 456.78
senbuff = struct.pack("=BBBf",part,action,index,times)
for request in range(1,5):
    print('send ',request,'...')
    socket.send(senbuff)
    message = socket.recv()
    print('received reply ',request,'[',message,']')

在ubuntu20.04上测试

编译C++版本server,X86端
g++ -g -Wall server.cpp -o serverX86 -lstdc++ -lzmq

编译生成serverX86可执行文件

编译C++版本client,X86端
g++ -g -Wall client.cpp -o clientX86 -lstdc++ -lzmq

编译生成clientX86可执行文件

运行测试

分别运行serverX86和client.py,server端将收到数据后应答,如下图所示


ubuntu20.04消息测试
编译C++版本server,ARM端
export PATH=$PATH:/home/mhming/Desktop/ZeroMQ/gcc4.8/bin
arm-linux-gnueabihf-g++ -I /home/mhming/Desktop/ZeroMQ/sources/arm-zeromq/include -L /home/mhming/Desktop/ZeroMQ/sources/arm-zeromq/lib -g -Wall server.cpp -o serverARM -lstdc++ -lzmq

说明:-I指定包含头文件路径,-L指定包含库文件路径。
编译完成后可以使用file命令查看生成的serverARM文件格式,正确的格式如下:


编译生成ARM端测试server

将上述生成的serverARM拷贝到ARM环境下运行即可。
这里方便测试我们可以将ARM环境和ubuntu20.04主机直连或者连接到同一个网络下,在ubuntu20.04主机下运行client.py客户端程序即可进行测试。
注意:这里需要将client.py中的localhost更改为ARM环境的IP地址。

编译C++版本client,ARM端
export PATH=$PATH:/home/mhming/Desktop/ZeroMQ/gcc4.8/bin
arm-linux-gnueabihf-g++ -I /home/mhming/Desktop/ZeroMQ/sources/arm-zeromq/include -L /home/mhming/Desktop/ZeroMQ/sources/arm-zeromq/lib -g -Wall client.cpp -o clientARM -lstdc++ -lzmq

相关文章

网友评论

      本文标题:ZeroMQ交叉编译到ARM设备上使用

      本文链接:https://www.haomeiwen.com/subject/fzgkhrtx.html