0


Linux下ZMQ的安装

一、编译安装

1.1、安装依赖

  1. sudo apt-get install libtool pkg-config build-essential autoconf automake

1.2、编译安装ZMQ使用的加密库

  1. git clone git://github.com/jedisct1/libsodium.git
  2. cd libsodium
  3. ./autogen.sh
  4. ./configure
  5. make check
  6. sudo make install
  7. sudo ldconfig
  8. cd ../

1.3、编译安装libzmq

  1. git clone git://github.com/zeromq/libzmq.git
  2. cd libzmq
  3. ./autogen.sh
  4. ./configure with-libsodium
  5. make
  6. sudo make install
  7. sudo ldconfig
  8. cd ../

1.4、安装ZMQ的c库

  1. 添加编译选项
  1. -lczmq -lzmq
  1. git clone git://github.com/zeromq/czmq.git
  2. cd czmq
  3. ./autogen.sh
  4. ./configure && make check
  5. sudo make install
  6. sudo ldconfig
  7. cd -

1.5、添加ZMQ的C++库

  1. git clone https://github.com/Microsoft/vcpkg.git
  2. cd vcpkg
  3. ./bootstrap-vcpkg.sh # bootstrap-vcpkg.bat for Powershell
  4. ./vcpkg integrate install
  5. ./vcpkg install cppzmq
  6. cd -

二、使用

2.1、cmake使用

  1. cmake下使用,需要再CMakeList文件中添加如下内容:
  1. #find cppzmq wrapper, installed by make of cppzmq
  2. find_package(cppzmq)
  3. target_link_libraries(*Your Project Name* cppzmq)

2.2、实例

  1. C++开发可以参考如下实例代码进行开发工作:
  2. server端:
  1. #include <zmq.hpp>
  2. int main()
  3. {
  4. zmq::context_t ctx;
  5. zmq::socket_t sock(ctx, zmq::socket_type::push);
  6. sock.bind("inproc://test");
  7. sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait);
  8. }
  1. Client端:
  1. #include <iostream>
  2. #include <zmq_addon.hpp>
  3. int main()
  4. {
  5. zmq::context_t ctx;
  6. zmq::socket_t sock1(ctx, zmq::socket_type::push);
  7. zmq::socket_t sock2(ctx, zmq::socket_type::pull);
  8. sock1.bind("tcp://127.0.0.1:*");
  9. const std::string last_endpoint =
  10. sock1.get(zmq::sockopt::last_endpoint);
  11. std::cout << "Connecting to "
  12. << last_endpoint << std::endl;
  13. sock2.connect(last_endpoint);
  14. std::array<zmq::const_buffer, 2> send_msgs = {
  15. zmq::str_buffer("foo"),
  16. zmq::str_buffer("bar!")
  17. };
  18. if (!zmq::send_multipart(sock1, send_msgs)) {
  19. return 1;
  20. }
  21. std::vector<zmq::message_t> recv_msgs;
  22. const auto ret = zmq::recv_multipart(
  23. sock2, std::back_inserter(recv_msgs));
  24. if (!ret) {
  25. return 1;
  26. }
  27. std::cout << "Got " << *ret
  28. << " messages" << std::endl;
  29. return 0;
  30. }
标签: linux 中间件 c++

本文转载自: https://blog.csdn.net/code_lyb/article/details/127984078
版权归原作者 Coder个人博客 所有, 如有侵权,请联系我们删除。

“Linux下ZMQ的安装”的评论:

还没有评论