linux zmq环境配置,CentOS配置Zmqpp

踩坑无数,废了好几个container,特此记录一下

Centos命令行起始是#,但因为#在代码片里会注释化,就改成$了

1. 预先安装dependency

$ yum install -y wget gcc-c++ automake autoconf libtool make lsof vim nano boost git

$ yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel curl-devel boost-devel readline-devel sqlite-devel kernel-devel

安装时遇到过GPG Keys Failed的错误

GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

Public key for perl-threads-2.21-2.el8.x86_64.rpm is not installed. Failing package is: perl-threads-1:2.21-2.el8.x86_64

GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

Public key for perl-threads-shared-1.58-2.el8.x86_64.rpm is not installed. Failing package is: perl-threads-shared-1.58-2.el8.x86_64

GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

The downloaded packages were saved in cache until the next successful transaction.

You can remove cached packages by executing 'dnf clean packages'.

Error: GPG check FAILED

因为用的是docker,于是删了容器重新开个容器就好了

解决问题的过程中搜到过一个可以尝试的方案,在我的电脑上没有用,不过或许可以帮到大家

2. 按照官方指令下载三个包

$ git clone git://github.com/jedisct1/libsodium.git

$ git clone git://github.com/zeromq/libzmq.git

$ git clone git://github.com/zeromq/zmqpp.git

3. Build libsodium,这个没有什么问题

$ cd libsodium

$ ./autogen.sh

$ ./configure && make check

$ make install // 我用的是docker container所以不需要sudo,没有权限的话要加上

$ ldconfig // 我用的是docker container所以不需要sudo,没有权限的话要加上

4. Build libzmq

$ cd libzmq

$ ./autogen.sh

这里如果直接跑 ./configure --with-libsodium && make 可能会遇到找不到libsodium的问题

$ ./configure --with-libsodium && make

............

checking for working volatile... yes

checking for sodium... no

./configure: line 421: test: then: integer expression expected

configure: error: run

./configure: line 310: return: then: numeric argument required

./configure: line 320: exit: then: numeric argument required

猜想是因为调用时无法查看到 libsodium 动态库的地址,于是查看

$ whereis libsodium

libsodium: /usr/local/lib/libsodium.so /usr/local/lib/libsodium.a /usr/local/lib/libsodium.la /usr/src/zmq/libsodium

果然,libsodium 并不在默认会查找的 /usr/lib 和 /lib 中,于是添加动态库地址并查看

$ echo "/usr/local/lib">>/etc/ld.so.conf

$ cat /etc/ld.so.conf

include ld.so.conf.d/*.conf

/usr/local/lib

然后就可以继续了

$ ./configure --with-libsodium && make

$ make install // 我用的是docker container所以不需要sudo,没有权限的话要加上

$ ldconfig // 我用的是docker container所以不需要sudo,没有权限的话要加上

5. Build zmqpp

记得 libzmq 的动态库地址也要include进 /etc/ld.so.conf

$ cd zmqpp

$ make

$ make check

$ sudo make install

$ make installcheck

6. 编译

取了官方simple_client和simple_server例子,想着终于装好了可以跑了,结果

$ g++ -o server server.cpp -lzmq

/tmp/ccOYmpjR.o: In function `main':

server.cpp:(.text+0x74): undefined reference to `zmqpp::socket::socket(zmqpp::context const&, zmqpp::socket_type)'

server.cpp:(.text+0x8a): undefined reference to `zmqpp::socket::bind(std::__cxx11::basic_string, std::allocator > const&)'

server.cpp:(.text+0x99): undefined reference to `zmqpp::message::message()'

server.cpp:(.text+0xb7): undefined reference to `zmqpp::socket::receive(zmqpp::message&, bool)'

server.cpp:(.text+0x15d): undefined reference to `zmqpp::socket::send(std::__cxx11::basic_string, std::allocator > const&, bool)'

server.cpp:(.text+0x193): undefined reference to `zmqpp::message::~message()'

server.cpp:(.text+0x1fa): undefined reference to `zmqpp::message::~message()'

server.cpp:(.text+0x20e): undefined reference to `zmqpp::socket::~socket()'

/tmp/ccOYmpjR.o: In function `zmqpp::context::~context()':

server.cpp:(.text._ZN5zmqpp7contextD2Ev[_ZN5zmqpp7contextD5Ev]+0x20): undefined reference to `zmqpp::context::terminate()'

/tmp/ccOYmpjR.o: In function `zmqpp::message& zmqpp::message::operator>><:__cxx11::basic_string std::char_traits>, std::allocator > >(std::__cxx11::basic_string, std::allocator >&)':

server.cpp:(.text._ZN5zmqpp7messagersINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERS0_RT_[_ZN5zmqpp7messagersINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERS0_RT_]+0x33): undefined reference to `zmqpp::message::get(std::__cxx11::basic_string, std::allocator >&, unsigned long) const'

collect2: error: ld returned 1 exit status

看出来了是找不到library,但到底是怎么个找不到法我思考了很久,终于找到了正确的写法

$ g++ -o server server.cpp /usr/local/lib/libzmqpp.a -lzmq

不可以少任何一项,也不可以把静态library放到前面写,直接引入-L和 pkg-config 也不管用,很奇怪

不过终于是真的好了

$ ./client

Connecting to hello world server…

Sending Hello 0…

Received World 0

Sending Hello 1…

Received World 1

Sending Hello 2…

Received World 2

Sending Hello 3…

Received World 3

Sending Hello 4…

Received World 4

$ ./server

Received Hello

Received Hello

Received Hello

Received Hello

Received Hello

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值