安装依赖
sudo apt-get install libtool pkg-config build-essential autoconf automake
编译安装ZMQ使用的加密库
sudo wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz
sudo tar -zxvf LATEST.tar.gz
cd libsodium-stable
./configure
make
make check
sudo make install
编译安装libzmq(去官网抄袭)
echo "deb http://download.opensuse.org/repositories/network:/messaging:/zeromq:/release-stable/Debian_9.0/ ./" >> /etc/apt/sources.list
wget https://download.opensuse.org/repositories/network:/messaging:/zeromq:/release-stable/Debian_9.0/Release.key -O- | sudo apt-key add
apt-get install libzmq3-dev
ubuntu 18.04 遇到问题
error: ‘zmq::send_flags’ has not been declared
参考一些文章
How to address the problme: “build error: ‘zmq::recv_flags’ has not been declared“_Drscq的博客-CSDN博客
waf build error: ‘zmq::recv_flags’ has not been declared · Issue #33 · tkn-tub/ns3-gym · GitHub
用以下方法解决
sudo apt-add-repository "deb http://in.archive.ubuntu.com/ubuntu/ focal-updates main restricted universe multiverse"
sudo apt-add-repository "deb-src http://in.archive.ubuntu.com/ubuntu/ focal-updates main restricted universe multiverse"
sudo apt-add-repository "deb http://archive.ubuntu.com/ubuntu focal main universe restricted multiverse"
sudo apt-add-repository "deb-src http://archive.ubuntu.com/ubuntu focal restricted universe multiverse main"
sudo apt-get update
sudo apt-get install libzmq3-dev
官方代码和构建命令
// server.cpp
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <string>
#include <iostream>
#ifndef _WIN32
#include <unistd.h>
#else
#include <windows.h>#define sleep(n) Sleep(n)
#endifint main () {// Prepare our context and socketzmq::context_t context (2);zmq::socket_t socket (context, zmq::socket_type::rep);socket.bind ("tcp://*:5555");while (true) {zmq::message_t request;// Wait for next request from clientsocket.recv (request, zmq::recv_flags::none);std::cout << "Received Hello" << std::endl;// Do some 'work'sleep(1);// Send reply back to clientzmq::message_t reply (5);memcpy (reply.data (), "World", 5);socket.send (reply, zmq::send_flags::none);}return 0;
}
// client.cpp
// Hello World client in C++
// Connects REQ socket to tcp://localhost:5555
// Sends "Hello" to server, expects "World" back
//
#include <zmq.hpp>
#include <string>
#include <iostream>int main ()
{// Prepare our context and socketzmq::context_t context (1);zmq::socket_t socket (context, zmq::socket_type::req);std::cout << "Connecting to hello world server..." << std::endl;socket.connect ("tcp://localhost:5555");// Do 10 requests, waiting each time for a responsefor (int request_nbr = 0; request_nbr != 10; request_nbr++) {zmq::message_t request (5);memcpy (request.data (), "Hello", 5);std::cout << "Sending Hello " << request_nbr << "..." << std::endl;socket.send (request, zmq::send_flags::none);// Get the reply.zmq::message_t reply;socket.recv (reply, zmq::recv_flags::none);std::cout << "Received World " << request_nbr << std::endl;}return 0;
}
g++ server.cpp -lzmq -o server
g++ client.cpp -lzmq -o client
完全照搬这篇文章也没问题
Linux下ZMQ的安装_zmq安装_Coder个人博客的博客-CSDN博客
使用cmake
cmake_minimum_required(VERSION 3.14.1)project(onlineapp)if(CMAKE_COMPILER_IS_GNUCC)message("COMPILER IS GNUCC")ADD_DEFINITIONS ( -std=c++11 )
endif(CMAKE_COMPILER_IS_GNUCC)SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -ggdb3")
#SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")# 5. 添加头文件路径
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
# include_directories(${CMAKE_SOURCE_DIR}/include)
# include_directories(${CMAKE_SOURCE_DIR}/soc_com_mgr/inc)
# 6. 添加源文件
FILE(GLOB_RECURSE SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
message(${CMAKE_CURRENT_SOURCE_DIR})
message(${SOURCE_FILES})
# 7. 添加链接库
find_package(cppzmq)# 9. 设置环境变量,编译用到的源文件全部都要放到这里,否则编译能够通过,
#但是执行的时候会出现各种问题,比如"symbol lookup error xxxxx , undefined symbol"
SET(ALL_SRCS ${SOURCE_FILES})
message(${ALL_SRCS})# 10.add executable file,添加要编译的可执行文件
ADD_EXECUTABLE(server server.cpp)
ADD_EXECUTABLE(client client.cpp)# 不可以
#TARGET_LINK_LIBRARIES(${CMAKE_BINARY_DIR}/module/libmodule.so)
target_link_libraries(server cppzmq)
target_link_libraries(client cppzmq)