MQTT在linux下环境下的编译与配置

         安装的方法参照http://blog.csdn.net/Netown_Ethereal/article/details/22653125

做MQTT的开发,记录下一些学习过程。

安装包括:

1、RPM安装

2、源码安装


(一)使用RPM安装

安装环境Centos6.4

从下面的链接下载mosquitto的RPM安装包:点击打开链接(http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-6/i686/)


运行安装命令:

[html]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">[root@localhost xxx]# rpm -i mosquitto-1.3.1-3.1.i686.rpm   
  2. warning: mosquitto-1.3.1-3.1.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 49e1d0b1: NOKEY</span>  

提示有warning,查了一下,这个warning是可以忽略的。方法是安下面的指令来安装:


[html]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">[root@localhost XXX]# rpm -Uhv mosquitto-1.3.1-3.1.i686.rpm   
  2. warning: mosquitto-1.3.1-3.1.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 49e1d0b1: NOKEY  
  3. Preparing...                ########################################### [100%]  
  4.     package mosquitto-1.3.1-3.1.i686 is already installed</span>  

如上所示,显示安装成功。

下面查看一下服务。

[html]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">[root@localhost /]# mosquitto  
  2. mosquitto         mosquitto_passwd  </span>  

在命令行输入:mosquitto,按两次Tab键,出现了下面两条指令。表示mosquitto已经安装成功。启动mosquitto服务的话只需要输入mosquitto指令即可。如下:
[html]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">[root@localhost /]# mosquitto  
  2. 1396234681: mosquitto version 1.3.1 (build date 2014-03-25 00:14:12+0000) starting  
  3. 1396234681: Using default config.  
  4. 1396234681: Opening ipv4 listen socket on port 1883.  
  5. 1396234681: Opening ipv6 listen socket on port 1883.  
  6. 1396234750: New connection from 192.168.139.80 on port 1883.  
  7. 1396234750: New client connected from 192.168.139.80 as qylMQTT (c1, k60).</span>  

最后一行qylMQTT是我在windows端开发的client id,表示已经从客户端连接到MQTT服务器了。


上面发现没有mosquitto的发布和订阅客户端,只有服务器。下面安装mosquitto的客户端。

1、从上面给的下载RPM包的地址下载mosquitto的客户端。

主要是以下几个RPM包:

libmosquitto1-1.3.1-3.1.i686.rpm   
mosquitto-clients-1.3.1-3.1.i686.rpm  

注意里面有好几个是c++的devel,不要下载错了,而且好像c++的devel也需要先安装 libmosquitto1-1.3.1-3.1.i686.rpm

2、按照上面提到的安装RPM包的方式安装上述两个RPM包

3、安装成功之后在命令行输入mosquitto,Tab两次,显示的命令如下:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto  
  2. mosquitto         mosquitto_passwd  mosquitto_pub     mosquitto_sub   


可见,已经安装上了client。


4、运行

打开服务器:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto  
  2. 1396235903: mosquitto version 1.3.1 (build date 2014-03-25 00:14:12+0000) starting  
  3. 1396235903: Using default config.  
  4. 1396235903: Opening ipv4 listen socket on port 1883.  
  5. 1396235903: Opening ipv6 listen socket on port 1883.  
  6. 1396235917: New connection from ::1 on port 1883.  
  7. 1396235917: New client connected from ::1 as mosqsub/1915-localhost. (c1, k60).  
  8. 1396235953: New connection from ::1 on port 1883.  
  9. 1396235953: New client connected from ::1 as mosqpub/2397-localhost. (c1, k60).  


订阅:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto_sub -t test -d  
  2. Client mosqsub/1915-localhost. sending CONNECT  
  3. Client mosqsub/1915-localhost. received CONNACK  
  4. Client mosqsub/1915-localhost. sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0)  
  5. Client mosqsub/1915-localhost. received SUBACK  
  6. Subscribed (mid: 1): 0  
  7. Client mosqsub/1915-localhost. received PUBLISH (d0, q0, r0, m0, 'test', ... (11 bytes))  
  8. hello world  
  9. Client mosqsub/1915-localhost. sending PINGREQ  
  10. Client mosqsub/1915-localhost. received PINGRESP  
  11. Client mosqsub/1915-localhost. sending PINGREQ  
  12. Client mosqsub/1915-localhost. received PINGRESP  
  13. Client mosqsub/1915-localhost. sending PINGREQ  
  14. Client mosqsub/1915-localhost. received PINGRESP  
  15. Client mosqsub/1915-localhost. sending PINGREQ  
  16. Client mosqsub/1915-localhost. received PINGRESP  


发布:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto_pub -t test -m "hello world" -d  
  2. Client mosqpub/2397-localhost. sending CONNECT  
  3. Client mosqpub/2397-localhost. received CONNACK  
  4. Client mosqpub/2397-localhost. sending PUBLISH (d0, q0, r0, m1, 'test', ... (11 bytes))  
  5. Client mosqpub/2397-localhost. sending DISCONNECT  


订阅一栏中显示的“hello world”即是所发布的消息。


下面是自己测试的vc++客户端发布消息的例程,先记下,回头再详细谈谈用vc++开发mosquitto客户端的方法。

服务器:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto  
  2. 1396245090: mosquitto version 1.3.1 (build date 2014-03-25 00:14:12+0000) starting  
  3. 1396245090: Using default config.  
  4. 1396245090: Opening ipv4 listen socket on port 1883.  
  5. 1396245090: Opening ipv6 listen socket on port 1883.  
  6. 1396245096: New connection from ::1 on port 1883.  
  7. 1396245096: New client connected from ::1 as mosqsub/19451-localhost (c1, k60).  
  8. 1396245108: New connection from 192.168.139.80 on port 1883.  
  9. 1396245108: New client connected from 192.168.139.80 as qylMQTT (c1, k60).  
  10. 1396245110: Socket error on client qylMQTT, disconnecting.  
  11. 1396245189: New connection from 192.168.139.80 on port 1883.  
  12. 1396245189: New client connected from 192.168.139.80 as qylMQTT (c1, k60).  
  13. 1396245190: Socket error on client qylMQTT, disconnecting.  


订阅:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto_sub -t test -d  
  2. Client mosqsub/19451-localhost sending CONNECT  
  3. Client mosqsub/19451-localhost received CONNACK  
  4. Client mosqsub/19451-localhost sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0)  
  5. Client mosqsub/19451-localhost received SUBACK  
  6. Subscribed (mid: 1): 0  
  7. Client mosqsub/19451-localhost received PUBLISH (d0, q0, r0, m0, 'test', ... (4 bytes))  
  8. Hell  
  9. Client mosqsub/19451-localhost sending PINGREQ  
  10. Client mosqsub/19451-localhost received PINGRESP  
  11. Client mosqsub/19451-localhost received PUBLISH (d0, q0, r0, m0, 'test', ... (48 bytes))  
  12. Hello world, this is a message from vc++ client.  

接收端第一次只收到了“Hell”,大概是因为发送的时候计算payload的长度时出错了。第二次接受即正常。


(二)源码安装

事实上从去年到今天最开始的时候我都是使用源码安装的mosquitto,但是今天上午安装最新版的mosquitto时,发现源码安装不上,然后为了尽快测试我的vc++客户端,才选用的RPM安装。下午集中解决了一下源码安装的问题。


1、系统:centos6.4

2、下载源码:下载地址(http://mosquitto.org/download/)

我下载的是最新更新的mosquitto-1.3.1.tar.gz


3、解压缩

tar zxvf mosquitto-1.3.tar.gz 


4、暂时用不到SSL,所以安装的时候我把SSL关闭了

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost mosquitto-1.3]# make WITH_TLS=no  
  2. set -e; for d in lib client src; do make -C ${d}; done  
  3. make[1]: Entering directory `/qyl/mosquitto-1.3/lib'  
  4. cc -Wall -ggdb -O2  -I. -I.. -I../lib -fPIC -DWITH_THREADING -DWITH_SRV -c mosquitto.c -o mosquitto.o  
  5. In file included from mosquitto.c:46:  
  6. mosquitto_internal.h:51:20: error: ares.h: No such file or directory  
  7. In file included from mosquitto.c:46:  
  8. mosquitto_internal.h:238: error: expected specifier-qualifier-list before ?.res_channel?  
  9. mosquitto.c: In function ?.osquitto_loop?.  
  10. mosquitto.c:834: error: ?.truct mosquitto?.has no member named ?.chan?  
  11. mosquitto.c:837: warning: implicit declaration of function ?.res_fds?  
  12. mosquitto.c:837: error: ?.truct mosquitto?.has no member named ?.chan?  
  13. mosquitto.c:917: error: ?.truct mosquitto?.has no member named ?.chan?  
  14. mosquitto.c:918: warning: implicit declaration of function ?.res_process?  
  15. mosquitto.c:918: error: ?.truct mosquitto?.has no member named ?.chan?  
  16. make[1]: *** [mosquitto.o] Error 1  
  17. make[1]: Leaving directory `/qyl/mosquitto-1.3/lib'  
  18. make: *** [mosquitto] Error 2  

在lib/mosquitto_internal.h里面找到ares.h,发现这个头文件是由宏定义WITH_SRV控制的。

不知道SRV SUPPORT是什么东西,但是查看mosquitto的更新日志,发现SRV support是今年3月16号才添加上的,我说去年源码安装一直没问题,今年怎么就不行了。

解决办法,在config.mk配置文件里面把srv support 关掉。如下图:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # Build with SRV lookup support.  
  2. WITH_SRV:=no  

再执行make,就可以顺利安装了。


5、验证

在命令行输入mosquitto,Tab两次:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost mosquitto-1.3]# mosquitto  
  2. mosquitto         mosquitto_passwd  mosquitto_pub     mosquitto_sub   

mosquitto服务器和client都安装成功了。需要注意的是:它依赖于一个DNS解析库。可以通过yum -y install c-ares-devel指令来进行相应的安装。


c-ares-devel.i686 : Development files for c-ares
c-ares-devel.x86_64 : Development files for c-ares

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Linux/ARM下编译安装Qt MQTT库,您可以按照以下步骤进行操作: 1. 首先,确保您已经在ARM架构的Linux系统上安装了必要的编译工具和依赖项,例如GCC、make等。可以通过包管理器或从源代码手动安装它们。 2. 下载Qt MQTT库的源代码,可以从Qt官方网站或其他可靠的资源下载。确保选择与您的Qt版本兼容的源代码。 3. 解压源代码文件,并进入解压后的目录。 4. 打开终端,进入源代码目录,并执行以下命令来生成Makefile: ``` qmake ``` 5. 接下来,使用make命令编译库文件: ``` make ``` 6. 编译过程可能会需要一些时间,具体取决于您的系统性能。请耐心等待编译完成。 7. 完成编译后,可以使用以下命令安装Qt MQTT库到系统中: ``` make install ``` 8. 安装完成后,您可以在系统的Qt库目录中找到MQTT库文件。可以通过以下命令获取库文件的位置: ``` qmake -query QT_INSTALL_LIBS ``` 9. 接下来,您可以在Qt项目中使用MQTT库。在项目的.pro文件中添加以下行: ``` LIBS += -lQt5Mqtt ``` 以上就是在Linux/ARM下编译安装Qt MQTT库的简要步骤。请注意,这只是一般的指导,具体步骤可能会因您所使用的系统和版本而有所差异。请参考Qt官方文档或相关资源获取更详细的信息。 ### 回答2: 在Linux/ARM平台下,编译安装Qt MQTT库的过程如下: 1. 首先,确保系统中已经安装了必要的开发工具链和依赖库。可以使用以下命令安装: ``` sudo apt-get update sudo apt-get install build-essential sudo apt-get install libssl-dev ``` 2. 下载Qt MQTT库的源代码。可以从Qt官方网站的下载页面或Github上的项目页面获取最新的源代码。 3. 解压下载的源代码。可以使用以下命令: ``` tar -xzvf qt-mqtt-xxx.tar.gz cd qt-mqtt-xxx ``` 4. 创建一个用于编译的目录,并进入该目录: ``` mkdir build cd build ``` 5. 配置编译选项。运行以下命令,选择所需的配置选项: ``` qmake .. ``` 你可以在此处添加其他自定义的配置选项。 6. 运行make命令进行编译: ``` make ``` 这将会生成Qt MQTT库的可执行文件。 7. (可选)运行make install命令进行安装。这将会将Qt MQTT库复制到系统目录中,以便其他项目可以使用: ``` sudo make install ``` 如果不想全局安装库,可以跳过此步骤,将生成的库文件手动复制到项目中使用。 完成上述步骤后,你就成功地在Linux/ARM平台下编译安装了Qt MQTT库。注意,具体的命令和选项可能会根据你所使用的Linux发行版和版本而有所不同,以上命令仅供参考。 ### 回答3: 在Linux/ARM下编译安装Qt MQTT库,需要按照以下步骤进行: 1. 首先,确保你已经安装了Qt开发环境和所需的编译工具链。你可以使用命令行工具或者图形界面工具进行安装。 2. 下载Qt MQTT库的源代码。你可以在Qt的官方网站上找到对应的版本。下载后,解压缩源代码到你的开发环境下的任意目录。 3. 打开终端,进入解压缩后的源代码目录。使用cd命令进行切换目录。 4. 创建一个新的构建目录,并进入该目录。这个目录将用于存储编译生成的文件。可以使用以下命令创建并进入该目录: ``` mkdir build cd build ``` 5. 使用qmake命令生成Makefile文件。执行以下命令: ``` qmake .. ``` 6. 使用make命令进行编译。执行以下命令: ``` make ``` 7. 编译完成后,可以使用make install命令进行安装。执行以下命令: ``` make install ``` 8. 安装完成后,你可以在系统的Qt库目录中找到编译生成的MQTT库文件。其中包括头文件和库文件。 9. 最后,你可以在你的项目中使用Qt MQTT库。在你的项目文件中添加相应的包含路径和链接库即可开始使用。 请注意,以上步骤中涉及到的命令和路径可能需要根据你的具体环境和需求进行调整。此外,确保你已经阅读并理解了Qt MQTT库的编译要求和依赖项,以确保顺利进行编译和安装。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值