概述
為了防止后來(lái)人走彎路本人綜合網(wǎng)上的各路教程,統(tǒng)計(jì)出一個(gè)自己成功方便的路徑,以便給其他有需求的人一些參考
一開(kāi)始是因?yàn)楣ぷ餍枨笮枰浦瞞qtt協(xié)議到嵌入式arm-linux上,使用的phao的mqtt支持包,一開(kāi)始看到phao官網(wǎng)上的說(shuō)明,便選擇embbed的嵌入式版本,但是嵌入式版本是不支持ssl的,可能是官網(wǎng)描寫(xiě)有誤,所以,要支持ssl,必須選擇標(biāo)準(zhǔn)的c版本進(jìn)行交叉編譯。
第一步
- 首先標(biāo)準(zhǔn)的c版本是需要OpenSSL的支持,我們先從 OpenSSL的官網(wǎng) 下載OpenSSL的最新版本 openssl-1.1.1-pre8.tar.gz
- 然后從github上下載zip包
- 在方便位置新建文件夾為template(推薦在主目錄)
mkdir ./template
-
將openssl-1.1.1-pre8.tar.gz和paho.mqtt.c-master.zip都放到template文件夾下備用
image.png
第二步
- 首先將OpenSSL和paho.mqtt.c解壓到當(dāng)前目錄
cd ./template
tar -xzvf openssl-1.1.1-pre8.tar.gz
unzip paho.mqtt.c-master.zip
解壓之后的情況應(yīng)該是這樣

- 先將OpenSSL進(jìn)行交叉編譯,這里我們需要修改Makefile
先執(zhí)行
./config no-asm -shared --prefix=/home/dai/template/ssl-arm
perfix是為了把編譯完成的文件存放在template下的ssl-arm目錄下,需要使用絕對(duì)地址,如果像改變存放地址,可以自己進(jìn)行修改,
gedit ./openssl-1.1.1-pre8/Makefile
ubuntu推薦使用圖形文本編輯器,如果是其他linux也可以使用vi編輯器
定位到90行將
CROSS_COMPILE=
改為
CROSS_COMPILE=arm-linux-
交叉編譯工具實(shí)際由你的目標(biāo)板來(lái)定
順便將123和124行的-m64刪除,我們這里不需要用到64編譯
然后保存并關(guān)閉,
接下來(lái)執(zhí)行
make -j3
如果出現(xiàn)錯(cuò)誤
crypto/threads_pthread.c: In function `CRYPTO_THREAD_lock_new':
crypto/threads_pthread.c:43: warning: implicit declaration of function `pthread_mutexattr_settype'
crypto/threads_pthread.c:43: error: `PTHREAD_MUTEX_RECURSIVE' undeclared (first use in this function)
crypto/threads_pthread.c:43: error: (Each undeclared identifier is reported only once
crypto/threads_pthread.c:43: error: for each function it appears in.)
Makefile:5122: recipe for target 'crypto/threads_pthread.o' failed
make[1]: *** [crypto/threads_pthread.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory '/home/dai/template/openssl-1.1.1-pre8'
Makefile:171: recipe for target 'all' failed
make: *** [all] Error 2
打開(kāi)crypro目錄下的threads_pthread.c,在18行添加一句
#define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
即可,這是因?yàn)橛行﹍inux版本沒(méi)有定義此變量造成的但是兩者意義相同。
接著再執(zhí)行
make install
就可以在ssl-arm文件下看到這些

到現(xiàn)在為止的話,OpenSSL的編譯算是告一段落了
第三部
編譯paho.mqtt.c
- 打開(kāi)paho.mqtt.c-master下的makefile文件
在第129行添加
CFLAGS += -I../ssl-arm/include -lrt
LDFLAGS += -L../ssl-arm/lib
為ssl編譯出來(lái)的路徑,如有自行修改,便自行修改
接下來(lái)執(zhí)行
make -j3 CC=arm-linux-gcc
接下來(lái)就大功告成了,只要在build文件找到動(dòng)態(tài)庫(kù)就可以了,當(dāng)然,simple里的例程也可以測(cè)試運(yùn)行以下。
