编译ARM版本mosquitto-1.6.8

编译前提依赖

需下载
c-ares-1.14.0

#下载源码
wget -c https://c-ares.haxx.se/download/c-ares-1.14.0.tar.gz
#解压源码
tar -zxvf c-ares-1.14.0.tar.gz
#进入源码 
cd c-ares-1.14.0/
#配置安装路径,编译链
./configure --host=arm-linux --prefix=/disk_4/mqtt/mosquitto-1.6.8/arm/ares
#编译
make
#安装 
make install

e2fsprogs-1.45.5

#从github下载源码
git clone https://github.com/tytso/e2fsprogs.git
 
cd e2fsprogs/
 
./configure --host=arm-linux --prefix=/disk_4/mqtt/mosquitto-1.6.8/arm/e2fsprogs
 
make
 
make install
# 把lib下的uuid目录拷贝到安装目/disk_4/mqtt/mosquitto-1.6.8/arm/e2fsprogs下
cp -r lib/uuid /disk_4/mqtt/mosquitto-1.6.8/arm/e2fsprogs/

openssl-1.1.1d

wget -c ftp://ftp.pca.dfn.de/pub/tools/net/openssl/source/openssl-1.1.1d.tar.gz

编译参考文档:https://blog.csdn.net/weixin_42892101/article/details/103830497
以上都交叉编译成arm版本

编译mosquitto库

#下载mosquitto源码
wget -c https://mosquitto.org/files/source/mosquitto-1.6.8.tar.gz
#解压
tar -xvf mosquitto-1.6.8.tar.gz
#进入mosquitto目录
cd mosquitto-1.6.8
#使用此命令配置交叉编译
cmake -D CMAKE_C_COMPILER="arm-none-linux-gnueabi-gcc" -D CMAKE_CXX_COMPILER="arm-none-linux-gnueabi-g++" -D CMAKE_INSTALL_PREFIX=/disk_4/mqtt/mosquitto-1.6.8/arm/mosquitto/ .
#编译
make

会提示找不到相关库文件

编辑`CMakeCach.txt`文件指定相关路径即可
#再次make
make
#安装
make install

指定路径修改处

编译ARM版本mosquitto-1.6.8_第1张图片

成果

编译ARM版本mosquitto-1.6.8_第2张图片
在这里插入图片描述
最后将其复制到需要的arm平台开发板上

1.6.10版本编译

替换为自己的交叉编译工具链
ps:编译到最后可能会出现无法安装
Install the project…
– Install configuration: “”
CMake Error at cmake_install.cmake:41 (file):
file INSTALL cannot find
“/home/aron566/Downloads/hf_mosquitto/source/mosquitto/man/mosquitto_passwd.1”.
虚拟机中找一下文件复制到源码的man目录中,最后make install即可!不过不搞这步也可以生成库文件
在这里插入图片描述
编译ARM版本mosquitto-1.6.8_第3张图片

#!/bin/sh
BUILD_HOST=/disk_4/toolchain/gcc-linaro-7.2.1-2017.11-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
ARM_GCC=${BUILD_HOST}gcc
ARM_CPP=${BUILD_HOST}g++
BASE=`pwd`
OUTPUT_PATH=${BASE}/install

OPENSSL=openssl-1.0.2t

make_dirs () {
    #为了方便管理,创建有关的目录
    cd ${BASE} && mkdir compressed install source -p
}

tget () { #try wget
    filename=`basename $1`
    echo "Downloading [${filename}]..."
    if [ ! -f ${filename} ];then
        wget $1
    fi

    echo "[OK] Downloaded [${filename}] "
}

tgit () { #try git and tar
    filename=`basename $1 | sed 's/.git//g'`

    echo "Clone [${filename}]..."
    if [ ! -f ${filename}.tgz ];then
        git clone $1
        rm ${filename}/.git* -rf
        echo "Making a tar file."
        tar -zcf ${filename}.tgz ${filename}
        rm ${filename} -rf
    fi

    echo "[OK] Cloned [${filename}] "
}

download_package () {
    cd ${BASE}/compressed
    #下载包
    tget  https://www.openssl.org/source/${OPENSSL}.tar.gz
    tgit  https://github.com/eclipse/paho.mqtt.c.git
    tgit  https://github.com/eclipse/mosquitto.git
}

tar_package () {
    cd ${BASE}/compressed
    ls * > /tmp/list.txt
    for TAR in `cat /tmp/list.txt`
    do
        tar -xf $TAR -C ../source
    done
    rm -rf /tmp/list.txt
}

pre_make_ssl () {
    cd ${BASE}/source/${OPENSSL}
    startLine=`sed -n '/install_html_docs\:/=' Makefile`
    echo $startLine
    # 为了避免 多行结果
    for startline in $startLine
    do
        lineAfter=99
        endLine=`expr $startline + 999`
        sed -i $startline','$endLine'd' Makefile
        echo "install_html_docs:" >> Makefile
        echo "\t@echo skip by Schips" >> Makefile
        echo "install_docs:" >> Makefile
        echo "\t@echo skip by Schips" >> Makefile
        echo "# DO NOT DELETE THIS LINE -- make depend depends on it." >> Makefile
        break
    done
}

make_ssl () {
    # 编译安装 zlib
    cd ${BASE}/source/${OPENSSL}
    rm ${OUTPUT_PATH}/${OPENSSL} -rf
    echo "SSL ABOUT"
    CC=${ARM_GCC} ./config no-asm shared --prefix=${OUTPUT_PATH}/${OPENSSL}

    sed 's/-m64//g'  -i Makefile # 删除-m64 关键字 (arm-gcc 不支持)
    #sudo mv /usr/bin/pod2man /usr/bin/pod2man_bak
    #mv doc/apps /tmp/
    pre_make_ssl
    make && make install
}

make_paho_mqtt_c () {
    cd ${BASE}/source/paho.mqtt.c

    make CFLAGS+="-I ${OUTPUT_PATH}/${OPENSSL}/include" \
         LDFLAGS+="-L${OUTPUT_PATH}/${OPENSSL}/lib" \
         CC=${ARM_GCC} \
         prefix=${OUTPUT_PATH}/paho.mqtt.c

    # BUG: make install 不符合我们的意愿
    rm ${OUTPUT_PATH}/paho.mqtt.c -rf
    mkdir ${OUTPUT_PATH}/paho.mqtt.c/lib -p
    mkdir ${OUTPUT_PATH}/paho.mqtt.c/bin -p
    mkdir ${OUTPUT_PATH}/paho.mqtt.c/include -p

    cp ${BASE}/source/paho.mqtt.c/build/output/lib*    ${OUTPUT_PATH}/paho.mqtt.c/lib -d -v
    cp ${BASE}/source/paho.mqtt.c/build/output/samples ${OUTPUT_PATH}/paho.mqtt.c/bin -r -v
    cp ${BASE}/source/paho.mqtt.c/build/output/test    ${OUTPUT_PATH}/paho.mqtt.c/bin -r -v
    cp ${BASE}/source/paho.mqtt.c/src/*.h       ${OUTPUT_PATH}/paho.mqtt.c/include    -v

}

make_mosquitto () {
    cd ${BASE}/source/mosquitto
	 	cmake -D CMAKE_C_COMPILER="${ARM_GCC}" -D CMAKE_CXX_COMPILER="${ARM_CPP}" -D CMAKE_INSTALL_PREFIX=${OUTPUT_PATH}/mosquitto \
					-D OPENSSL_CRYPTO_LIBRARY=${OUTPUT_PATH}/${OPENSSL}/lib/libcrypto.so -D OPENSSL_INCLUDE_DIR=${OUTPUT_PATH}/${OPENSSL}/include \
					-D OPENSSL_SSL_LIBRARY=${OUTPUT_PATH}/${OPENSSL}/lib/libssl.so -D CMAKE_C_FLAGS=-lpthread .
	make WITH_TLS=yes WITH_DOCS=no
	make install
}

make_dirs
download_package
tar_package
make_ssl
make_paho_mqtt_c
make_mosquitto
exit $?

你可能感兴趣的:(移植应用,mqtt,mosquitto)