Ubuntu zmq编译安装与arm移植

1 linux

下载libzmq source code:https://github.com/zeromq/libzmq/tree/v4.3.4

编译

sudo apt-get install libtool pkg-config build-essential autoconf automake # 安装依赖

  • ./autogen.sh
  • ./configure --without-libsodium
    • libsodium - 加密库
  • make -j4
  • sudo make install
  • sudo ldconfig

示例

server
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
 
int main (void)
{
    
    
    void *context = zmq_ctx_new ();
    void *responder = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (responder, "tcp://*:5555");
    assert (rc == 0);
 
    while (1) {
    
    
        char buffer [10];
        zmq_recv (responder, buffer, 10, 0);
        printf ("Received Hello\n");
        sleep (1);          //  Do some 'work'
        zmq_send (responder, "World", 5, 0);
    }
    return 0;
}

client

#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
 
int main (void)
{
    
    
    printf ("Connecting to hello world server…\n");
    void *context = zmq_ctx_new ();
    void *requester = zmq_socket (context, ZMQ_REQ);
    zmq_connect (requester, "tcp://localhost:5555");
    int request_nbr;
    for (request_nbr = 0; request_nbr != 10; request_nbr++) {
    
    
        char buffer [10];
        printf ("Sending Hello %d…\n", request_nbr);
        zmq_send (requester, "Hello", 5, 0);
        zmq_recv (requester, buffer, 10, 0);
        printf ("Received World %d\n", request_nbr);
    }
    zmq_close (requester);
    zmq_ctx_destroy (context);
    return 0;
}

执行

  • gcc -o client client.c -lzmq # 编译client
  • gcc -o server server.c -lzmq # 编译server

./server # 执行
./client

2 arm64

下载libzmq source code:https://github.com/zeromq/libzmq/tree/v4.3.4

编译

sudo apt-get install libtool pkg-config build-essential autoconf automake # 安装依赖

  • mkdir …/outs
  • ./autogen.sh
  • ./configure --host=aarch64-linux-gnu --prefix=`pwd`/…/outs --without-libsodium
    • host - 指定开发板上用的交叉编译工具
      • aarch64-linux-gnu - 当前系统内用于开发板的交叉编译工具,注意结尾不要加“-”
    • prefix - 指定编译完成的库输出目录
    • `pwd`/…/outs - 当前目录下的上级目录中的outs目录
    • libsodium - 加密库
  • make -j4
  • make install

示例

publish

#include<stdio.h>
#include<zmq.h>
#include<unistd.h>
#include<string.h>
 
int main(void)
{
    
    
        void *ctx,*sock;
        int ret = 0;
        char data[1024];
        int i = 0;
        ctx = zmq_ctx_new();
        sock = zmq_socket(ctx,ZMQ_PUB);
        ret = zmq_bind(sock,"tcp://127.0.0.1:5555");
        while(1) {
    
    
                sprintf(data,"[%d]PUB: Hello World",i++);
                printf("send data: %s\n", data);
                ret = zmq_send(sock,data,strlen(data),0);
                sleep(1);
        }
        zmq_close(sock);
        zmq_ctx_destroy(ctx);
        return 0;
}

subscribe

#include<stdio.h>
#include<zmq.h>
#include<unistd.h>
#include<string.h>
 
int main(void)
{
    
    
        void *ctx,*sock;
        int ret = 0;
        char data[1024];
        ctx = zmq_ctx_new();
        sock = zmq_socket(ctx,ZMQ_SUB);
        zmq_setsockopt(sock,ZMQ_SUBSCRIBE,"",0);
        ret = zmq_connect(sock,"tcp://127.0.0.1:5555");
        while(1) {
    
    
                bzero(data,sizeof(data));
                if(ret = zmq_recv(sock,data,sizeof(data)-1,0)<0)
                        printf("SUB : zmq_recv faild");
                printf("SUB:recv msg : %s\n",data);
                sleep(1);
        }
        zmq_close(sock);
        zmq_ctx_destroy(ctx);
        return 0;
}

执行

$ tree # 当前目录结构
── bin
│ └── curve_keygen
├── include
│ ├── zmq.h
│ └── zmq_utils.h
├── lib
│ ├── libzmq.a
│ ├── libzmq.la
│ ├── libzmq.so -> libzmq.so.5.2.4
│ ├── libzmq.so.5 -> libzmq.so.5.2.4
│ ├── libzmq.so.5.2.4
│ └── pkgconfig
│ └── libzmq.pc
├── publish.c
└── subscribe.c

用于开发板上执行

  • aarch64-linux-gnu-gcc -o publish publish.c -L./lib -lzmq
    • -L/path 以上-L./lib 表示在当前目录下的lib目录
    • -lxxx 把库文件的lib和扩展名去掉,,所以库文件libzmq.so,就可以直接用lzmq了
  • gcc -o subscribe subscribe.c -lzmq

./publish # 开发板上执行
./subscribe # 本机执行

猜你喜欢

转载自blog.csdn.net/tianzong2019/article/details/121982375