10.MQTT源码交叉编译与移植

基于MQTT源码的嵌入式LINUX移植,网上可参看资料几乎没有,估计是用的不多,没什么人弄,又或者
太简单,没人写参考。这里记录下编译与移植过程。大致有三部分,交叉编译openssl、交叉编译MQTT、安装MQTT服务器。

1 交叉编译OPENSSL

因为MQTT用到OPENSSL库,所以编译MQTT的时候要先编译OPENSSL。 解压源文件,创建openssl安装目录,配置openssl Makefile编译,安装。
CC = arm-fsl-linux-gnueabi-gccAR = arm-fsl-linux-gnueabi-ar RANLIB = arm-fsl-linux-gnueabi-ranlibINSTALLTOP = /home/MQTT/openssl OPENSSLDIR = /home/MQTT/openssl
$ tar zxvf openssl-0.9.8e.tar.gz
$ cd openssl-0.9.8e
$ mkdir /home/QMTT/openssl
$ gedit Makefile
$ make
$ make install
编译无误会在openssl目录下生成头文件,库文件等,编译MQTT的时候,指定这个头文件库文件路径即可

2 交叉编译MQTT

下载源码,解压,修改Makefile,交叉编译http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.c.git/snapshot/org.eclipse.paho.mqtt.c-1.0.3.tar.gz

CC = arm-fsl-linux-gnueabi-gcc CFLAGS += -I/home/MQTT/openssl/includeLDFLAGS += -L/home/MQTT/openssl/lib

$ tar -jxvf eclipse-paho-mqtt-unix-1.0.3.tar.bz
$ cd org.eclipse.paho.mqtt.c-1.0.3
$ gedit Makefile 
$ make

CFLAGS LDFLAGS是指定openssl库路径,不指定的话编译器会提示找不到ssl库,make完成后会在build目录下生成MQTT动态库文件及一些应用的例子可执行文件,可以用于测试。把目录下libxx.so.xx复制到目标板/usr/lib目录下,把/build/output/sample 可执行文件复制到目标板/opt目录用于测试。

# mount -t nfs -o nolock 192.168.1.110:/home/zhu /mnt
# cd /mnt/zhu/build
# cp /output lib* /usr/lib
# cp -rf /output/sample /opt

3 搭建MQTT服务器,测试

Linux下搭建mosquitto服务器,参考教程 http://blog.csdn.net/xukai871105/article/details/39252653
在linux主机上安装,不需要交叉编译,直接make,make install即可,提示找不到.h文件的时候,安装相应库文件即可。安装完成后还要修改下配置文件,增加mosquitto用户。
$cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
$useradd mosquitto
$mosquitto -v
mosquitto -v打开mosquitto服务,真确打开,终端会显示
1455613844: mosquitto version 1.4.8 (build date 2016-02-16 12:47:49+0800) starting
1455613844: Using default config.
1455613844: Opening ipv4 listen socket on port 1883.
1455613844: Opening ipv6 listen socket on port 1883.
1455613856: New connection from 192.168.1.136 on port 1883.

目标板上运行可执行文件测试
#./opt/sample/subasync
正确的话开发板终端会显示:
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Message with token value 1 delivery confirmed
linux主机终端显示:
1455613856: New client connected from 192.168.1.136 as ExampleClientSub (c1, k20).
1455613856: Sending CONNACK to ExampleClientSub (0, 0)
1455613856: Received SUBSCRIBE from ExampleClientSub
1455613856: 	MQTT Examples (QoS 1)
1455613856: ExampleClientSub 1 MQTT Examples
1455613856: Sending SUBACK to ExampleClientSub
出现无法连接错误,可能是由于mosquitto服务没有开启,或者是应用程序的IP地址不是mosquitto服务端的IP地址。修改相应文件的代码即可,以subasync.c为例,修改
#define ADDRESS     "tcp://localhost:1883"为 #define ADDRESS     "tcp://192.168.1.110:1883"
192.168.1.110为mosquitto服务端IP地址。

注:也可以直接运行 MQTTAsync_publish文件而不搭建mosquitto服务器,MQTTAsync_publish文件直接从m2m.eclipse.org服务器获取数据,这里开发板ping不通外网,所以没成功。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Building on: linux-g++ (x86_64, CPU features: mmx sse sse2) Building for: linux-aarch64-gnu-g++ (arm64, CPU features: neon) Target compiler: gcc 6.3.1 Configuration: cross_compile use_gold_linker compile_examples enable_new_dtags largefile neon precompile_header shared rpath release c++11 c++14 concurrent dbus reduce_exports stl Build options: Mode ................................... release Optimize release build for size ........ no Building shared libraries .............. yes Using C standard ....................... C11 Using C++ standard ..................... C++14 Using ccache ........................... no Using gold linker ...................... yes Using new DTAGS ........................ yes Using precompiled headers .............. yes Using LTCG ............................. no Target compiler supports: NEON ................................. yes Build parts ............................ libs Qt modules and options: Qt Concurrent .......................... yes Qt D-Bus ............................... yes Qt D-Bus directly linked to libdbus .... no Qt Gui ................................. yes Qt Network ............................. yes Qt Sql ................................. yes Qt Testlib ............................. yes Qt Widgets ............................. yes Qt Xml ................................. yes Support enabled for: Using pkg-config ....................... yes udev ................................... no Using system zlib ...................... yes Qt Core: DoubleConversion ....................... yes Using system DoubleConversion ........ no GLib ................................... no iconv .................................. yes ICU .................................... no Tracing backend ........................ Logging backends: journald ............................. no syslog ............................... no slog2 ................................ no Using system PCRE2 ..................... no Qt Network: getifaddrs() ........................... yes IPv6 ifname ............................ yes libproxy ............................... no Linux AF_NETLINK ....................... yes OpenSSL ................................ yes Qt directly linked to OpenSSL ........ no OpenSSL 1.1 ............................ no DTLS ................................... yes SCTP ................................... no Use system proxies ..................... yes Qt Gui: Accessibility .......................... yes FreeType ............................... yes Using system FreeType ................ no HarfBuzz ............................... yes Using system HarfBuzz ................ no Fontconfig ............................. no Image formats: GIF .................................. yes ICO .................................. yes JPEG ................................. yes Using system libjpeg ............... yes PNG .................................. yes Using system libpng ................ no EGL .................................... no OpenVG ................................. no OpenGL: Desktop OpenGL ....................... no OpenGL ES 2.0 ........................ no OpenGL ES 3.0 ........................ no OpenGL ES 3.1 ........................ no OpenGL ES 3.2 ........................ no Vulkan ................................. no Session Management ..................... yes Features used by QPA backends: evdev .................................. yes libinput ............................... no INTEGRITY HID .......................... no mtdev .................................. no tslib .................................. no xkbcommon .............................. no X11 specific: XLib ................................. no EGL on X11 ........................... no QPA backends: DirectFB ............................... no EGLFS .................................. no LinuxFB ................................ yes VNC .................................... yes Mir client ............................. no Qt Sql: SQL item models ........................ yes Qt Widgets: GTK+ ................................... no Styles ................................. Fusion Windows Qt PrintSupport: CUPS ................................... no Qt Sql Drivers: DB2 (IBM) .............................. no InterBase .............................. no MySql .................................. no OCI (Oracle) ........................... no ODBC ................................... no PostgreSQL ............................. no SQLite2 ................................ no SQLite ................................. yes Using system provided SQLite ......... no TDS (Sybase) ........................... no Qt Testlib: Tester for item models ................. yes Qt SerialBus: Socket CAN ............................. yes Socket CAN FD .......................... yes Qt QML: QML network support .................... yes QML debugging and profiling support .... yes QML sequence object .................... yes QML list model ......................... yes QML XML http request ................... yes QML Locale ............................. yes QML delegate model ..................... yes Qt Quick: Direct3D 12 ............................ no AnimatedImage item ..................... yes Canvas item ............................ yes Support for Qt Quick Designer .......... yes Flipable item .......................... yes GridView item .......................... yes ListView item .......................... yes TableView item ......................... yes Path support ........................... yes PathView item .......................... yes Positioner items ....................... yes Repeater item .......................... yes ShaderEffect item ...................... yes Sprite item ............................ yes Qt Scxml: ECMAScript data model for QtScxml ...... yes Qt Gamepad: SDL2 ................................... no Qt 3D: Assimp ................................. yes System Assimp .......................... no Output Qt3D Job traces ................. no Output Qt3D GL traces .................. no Use SSE2 instructions .................. no Use AVX2 instructions .................. no Aspects: Render aspect ........................ yes Input aspect ......................... yes Logic aspect ......................... yes Animation aspect ..................... yes Extras aspect ........................ yes Qt 3D Renderers: OpenGL Renderer ........................ yes Qt 3D GeometryLoaders: Autodesk FBX ........................... no Qt Wayland Client ........................ no Qt Wayland Compositor .................... no Qt Bluetooth: BlueZ .................................. no BlueZ Low Energy ....................... no Linux Crypto API ....................... no WinRT Bluetooth API (desktop & UWP) .... no Qt Sensors: sensorfw ............................... no Qt Quick Controls 2: Styles ................................. Default Fusion Imagine Material Universal Qt Quick Templates 2: Hover support .......................... yes Multi-touch support .................... yes Qt Positioning: Gypsy GPS Daemon ....................... no WinRT Geolocation API .................. no Qt Location: Qt.labs.location experimental QML plugin . yes Geoservice plugins: OpenStreetMap ........................ yes HERE ................................. yes Esri ................................. yes Mapbox ............................... yes MapboxGL ............................. no Itemsoverlay ......................... yes QtXmlPatterns: XML schema support ..................... yes Qt Multimedia: ALSA ................................... no GStreamer 1.0 .......................... no GStreamer 0.10 ......................... no Video for Linux ........................ yes OpenAL ................................. no PulseAudio ............................. no Resource Policy (libresourceqt5) ....... no Windows Audio Services ................. no DirectShow ............................. no Windows Media Foundation ............... no Qt Tools: QDoc ................................... no Qt WebEngine: Embedded build ......................... yes Pepper Plugins ......................... no Printing and PDF ....................... no Proprietary Codecs ..................... no Spellchecker ........................... yes Native Spellchecker .................... no WebRTC ................................. no Use System Ninja ....................... no Geolocation ............................ yes WebChannel support ..................... yes Use v8 snapshot ........................ yes Kerberos Authentication ................ no Building v8 snapshot supported ......... yes Use ALSA ............................... no Use PulseAudio ......................... no Optional system libraries used: re2 .................................. no icu .................................. no libwebp, libwebpmux and libwebpdemux . no opus ................................. no ffmpeg ............................... no libvpx ............................... no snappy ............................... no glib ................................. no zlib ................................. yes minizip .............................. no libevent ............................. no jsoncpp .............................. no protobuf ............................. no libxml2 and libxslt .................. no lcms2 ................................ no png .................................. no JPEG ................................. no harfbuzz ............................. no freetype ............................. no x11 .................................. no Required system libraries: fontconfig ........................... no dbus ................................. no nss .................................. no khr .................................. no glibc ................................ yes Required system libraries for qpa-xcb: libdrm ............................... no xcomposite ........................... no xcursor .............................. no xi ................................... no xrandr ............................... no xtst ................................. no Note: Also available for Linux: linux-clang linux-icc
paho.mqtt.embedded-c是一个用于开发嵌入式设备的MQTT C语言交叉编译是指在一台开发机上编译运行不同架构的目标设备上的程序。对于paho.mqtt.embedded-c的交叉编译,我们可以按照以下步骤进行: 1. 下载交叉编译工具链:根据目标设备的架构,从相应的厂商或开源社区下载对应的交叉编译工具链。这个工具链包含了可以在开发机上编译和调试目标设备上的程序所需的编译器、链接器和文件。 2. 配置交叉编译环境:将下载的交叉编译工具链添加到开发机的环境变量中,以便可以在命令行终端中访问到这些工具。在Linux系统中,可以通过修改.bashrc或者.profile文件来添加环境变量。在Windows系统中,可以在系统设置中进行相应配置。 3. 下载paho.mqtt.embedded-c源代码:从paho.mqtt.embedded-c的官方仓或者其他可信的源代码中下载最新的版本。 4. 配置交叉编译参数:进入paho.mqtt.embedded-c源代码所在的目录,并按照目标设备的架构和交叉编译工具链的要求,配置交叉编译参数。这包括编译器路径、文件路径、目标设备架构等。 5. 执行交叉编译命令:在命令行终端中执行交叉编译命令,将paho.mqtt.embedded-c源代码编译为目标设备可执行的二进制文件。这个命令通常使用交叉编译工具链提供的编译器和链接器,同时会指定一些编译选项,如优化级别、生成的目标文件名等。 6. 导出二进制文件:编译成功后,将生成的二进制文件导出到目标设备中,可以通过网络或者其他传输方式将其复制到目标设备的文件系统中。 通过以上步骤,我们可以将paho.mqtt.embedded-c交叉编译为可以在目标设备上运行的程序。这样,我们就可以在嵌入式设备中使用MQTT协议进行通信了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值