5.MQTT再学习 -- 交叉编译与移植

先说明一下,遇到的问题。我之前在 Ubuntu12.04 gcc 下可以搭建 mqtt 服务器生成的 libmosquitto.so.1。

参看:MQTT再学习 -- 搭建MQTT服务器及测试

现在我要在 DM368 的交叉编译器 arm-none-linux-gnueabi-gcc  链接共享库 libmosquitto.so.1。出现问题   cannot find -lmosquitto 即共享库找不到。

然后我想可能是编译器不匹配 ,然后想用 DM368的交叉编译器arm-none-linux-gnueabi-gcc 再安装一遍 mqtt 服务器让其生成 共享库  libmosquitto.so.1,然后就make出现了一堆的错误。


这上面是我在群里发帖咨询的遇到的问题,现在最终成功解决!!

现在就讲一下是怎么操作的。

一、准备工作

上面说的编译不同造成的无法链接共享库这个是真的,需要重新 make 生成 libmosquitto.so.1。
我们现在的虚拟机上有安装交叉编译器  arm-none-linux-gnueabi-gcc 需要查看一下它所在的位置
  1. # which arm-none-linux-gnueabi-gcc
  2. /opt/arm -2009q1 -203/bin/arm-none-linux-gnueabi-gcc
然后下载需要的软件
主要是两个 mosquitto-1.4.14 和 openssl-1.0.1c
下载:openssl-1.0.1c
下载:mosquitto-1.4.14

二、MQTT源码交叉编译

(1)编译需要依赖的openssl

将上面下载的文件 openssl-1.0.1c.tar.gz 解压
tar -zxvf openssl-1.0.1c.tar.gz
然后进入该文件
cd openssl-1.0.1c
然后执行:
./config no-asm shared --prefix=/opt/com/ssl
注意 :  /opt/com/ssl/ 文件夹是指定存放共享库的位置,你自己要先创建好的哦
然后修改Makefile中下列变量的值:
CC = /opt/arm-2009q1-203/bin/arm-none-linux-gnueabi-gcc 
AR = /opt/arm-2009q1-203/bin/arm-none-linux-gnueabi-ar $(ARFLAGS) r
RANLIB = /opt/arm-2009q1-203/bin/arm-none-linux-gnueabi-ranlib

注意:  这里就是为什么上面要你查看交叉编译所在位置的原因了。
再有啊,记得需要将之前的变量值注释掉的哦。 我第一次就忘记了,然后再编译的时候出现错误:
Relocations in generic ELF (EM: 3) libcrypto.a(e_old.o): could not read symbols: File in wrong format
解决方法,就是执行: #make clean -w

最后就是执行编译:

make && make install

(2)编译mosquitto-1.4.14客户端

将上面下载的文件 mosquitto-1.4.14.tar.gz 解压

tar -zxvf mosquitto-1.4.14.tar.gz

然后进入该文件
cd mosquitto-1.4.14
修改config.mk
WITH_SRV:=no
WITH_UUID:=no
WITH_WEBSOCKETS:=no
WITH_DOCS:=no
CFLAGS += -I/opt/com/ssl/include
LDFLAGS += -L/opt/com/ssl/lib -lssl -lcrypto
STRIP?=arm-none-linux-gnueabi-strip
注意: /opt/com/ssl/ 上面说了是共享库文件所在位置,你自己创建的。不过现在里面是有东西的。



执行编译
make CC=/opt/arm-2009q1-203/bin/arm-none-linux-gnueabi-gcc CXX=/opt/arm-2009q1-203/bin/arm-none-linux-gnueabi-g++
最后执行 
make install
最终在 /usr/locat/lib 目录下可以看到新生成的 libmosquitto.so.1
 
注意:  如果不修改 STRIP?=arm-none-linux-gnueabi-strip
会出现错误:strip: Unable to recognise the format of the input file `/usr/local/lib/libmosquitto.so.1'
这是参考那篇文章里所没有提到的。

三、总结

最后想不想看一下,gcc 和 arm-none-linux-gnueabi-gcc 编译生成的  libmosquitto.so.1 到底有啥不同。
gcc编译:
  1. # file libmosquitto.so.1
  2. libmosquitto.so .1: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked,
  3. BuildID[sha1]= 0xb05795243eded68d65abe39b12212099c849965b, not stripped
arm-none-linux-gnueabi-gcc 交叉编译:
  1. # file libmosquitto.so.1
  2. libmosquitto.so .1: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked,
  3. not stripped
看出来了么? 是类型啦,主要是在 Intel 80386 执行还是在 ARM 执行的。

猜你喜欢

转载自blog.csdn.net/u011124985/article/details/80829142