在cmake工程中使用ZeroMQ


ZeroMQ官網

linux平台

一、安裝libzmq

        1、下載代碼:git clone https://github.com/zeromq/libzmq.git

        2、切換到最新的tag,目前是v4.3.3:git checkout v4.3.3

        3、配置:

  1. cd libzmq
  2. ./autogen.sh
  3. ./configure --prefix=/home/xxx/libzmq/_install

        4、編譯並安裝:make -j4 && sudo make install

在配置時根據需求更改安裝目錄,如果是安裝在linux平台上可以直接安裝到/usr

 若是交叉編譯到ARM-LINUX上,要在Makefile中修改編譯工具鏈,並安裝到自己指定的目錄下

只需要安裝libzmq就可以使用ZeroMQ的通信功能了,但是由於libzmq本身是用C寫的,為了方便C++用戶,官方有做了一個針對C++的接口封裝,方便C++用戶使用C++類和對象進行編程。可以下載和編譯cppzmq或在zmqpp庫。

二、安裝zmqpp

       因為zmqpp是對libzmq接口的高層次的封裝,是c++接口中最好用的一個,所以選擇了zmqpp庫

       1、下載代碼:git clone https://github.com/zeromq/zmqpp.git

        2、切換到最新的tag,目前是v4.2.0:git checkout v4.2.0

        3、編譯並安裝:make -j4 && sudo make install

同樣需要安裝到指定目錄或在交叉編譯,在Makefile中指定

 

三、在cmake工程中使用zmqpp

 1. 在third_party添加zmqpp相關頭文件

 

 2. 在lib目錄下添加靜態庫zmq, zmqpp

 

 3. 在cmakelists中添加配置

...
include_directories(net lib)

link_libraries(
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/libzmq.a
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/libzmqpp.a
    pthread )

add_executable(server server.cpp)

add_executable(client client.cpp)

target_link_libraries(server pthread  zmqpp zmq)
...

4. 在源碼中添加頭文件

 1 #include "third_party/zmqpp/zmqpp.hpp"
 2 #include <string>
 3 #include <iostream>
 4 #include <chrono>
 5 #include <thread>
 6 
 7 using namespace std;
 8 
 9 int main(int argc, char *argv[]) {
10   const string endpoint = "tcp://*:5555";
11 
12   // initialize the 0MQ context
13   zmqpp::context context;
14 
15   // generate a pull socket
16   zmqpp::socket_type type = zmqpp::socket_type::reply;
17   zmqpp::socket socket (context, type);
18 
19   // bind to the socket
20   socket.bind(endpoint);
21   while (1) {
22     // receive the message
23     zmqpp::message message;
24     // decompose the message 
25     socket.receive(message);
26     string text;
27     message >> text;
28 
29     //Do some 'work'
30     std::this_thread::sleep_for(std::chrono::seconds(1));
31     cout << "Received Hello" << endl;
32     socket.send("World");
33   }
34 
35 }
server.cpp
 1 #include "third_party/zmqpp/zmqpp.hpp"
 2 #include <string>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int main(int argc, char *argv[]) {
 8   const string endpoint = "tcp://localhost:5555";
 9 
10   // initialize the 0MQ context
11   zmqpp::context context;
12 
13   // generate a push socket
14   zmqpp::socket_type type = zmqpp::socket_type::req;
15   zmqpp::socket socket (context, type);
16 
17   // open the connection
18   cout << "Connecting to hello world server…" << endl;
19   socket.connect(endpoint);
20   int request_nbr;
21   for (request_nbr = 0; request_nbr != 10; request_nbr++) {
22     // send a message
23     cout << "Sending Hello " << request_nbr <<"" << endl;
24     zmqpp::message message;
25     // compose a message from a string and a number
26     message << "Hello";
27     socket.send(message);
28     string buffer;
29     socket.receive(buffer);
30     
31     cout << "Received World " << request_nbr << endl;
32   }
33 }
client.cpp

5. 測試

參考:

跟我一起學習ZeroMQ(3):安裝libzmq和zmqpp(接下來基於zmqpp實現多種zmq通訊模式)

ZeroMQ推薦博客:

 

https://blog.csdn.net/lianshaohua


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM