2
)设置环境变量
sudo vi /etc/bash.bashrc
在文件最后添加以下配置
export PATH=/opt/openwrt-toolchain-ramips-mt76x8_gcc-7.3.0_musl.Linux-x86_64/toolchain-mipsel_24kc_gcc-7.3.0_musl/bin:$PATH
最后保存退出。
接着在终端执行以下命令:
source /etc/bash.bashrc
3
)检查是否安装成功
mipsel-openwrt-linux-gcc -v
此时应打印出交叉编译工具的一些信息,表示安装成功
二、交叉编译MQTT静态库
1)进入
根目录执行cmake,如果失败可能是没有安装cmake,执行apt install cmake安装cmake
2)执行cmake
cmake . -DPAHO_BUILD_STATIC=TRUE -DCMAKE_C_COMPILER=mipsel-openwrt-linux-gcc -DPAHO_WITH_SSL=FALSE
mipsel-openwrt-linux-gcc是将静态库交叉编译成openwrt下可以运行的库
执行cmake过程中可能会遇到错误
-- The CXX compiler identification is unknown
CMake Error at test/CMakeLists.txt:1 (PROJECT):
No CMAKE_CXX_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
原因是没有安装gcc和g++编译环境。安装方法很简单,一行命令搞定。在终端输入sudo apt-get install build-essential进行安装即可。为了查看是否安装上了,可以依次输入gcc --version、g++ --version来查看安装的gcc和g++的版本号。如果输出如下信息,则表明安装成功:
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
安装成功后,需要重启刚终端,然后再次输入cmake ..即可生成make文件
3)执行make命令
4)然后就可以在src目录下找到你的静态库了
5)静态库编译,使用Cmake
先修改CMakeLists.txt。
进到mqtt库目录:
编译生成的库路径:
安装库到ubuntu
静态库安装:
库的简单测试
这里使用异步库做测试
拷贝example到测试目录:
$:cp ~/work/mqtt/paho.mqtt.c-master/src/samples/MQTTAsync_*.c ./
$: gcc MQTTAsync_subscribe.c -o MQTTAsync_subscribe -L/usr/local/lib -lpaho-mqtt3a-static -lpthread
$: gcc MQTTAsync_publish.c -o MQTTAsync_publish -L/usr/local/lib -lpaho-mqtt3a-static -lpthread