一、參考資料
OpenSSL&&libcurl庫的交叉編譯 - hesetone - 博客園
二、準備工作
1. 編譯環境
- 宿主機:Ubuntu 20.04.6 LTS
- Host:ARM32位
- 交叉編譯器:
arm-linux-gnueabihf-gcc-11.1.0
2. 設置交叉編譯工具鏈
在交叉編譯之前,需要設置交叉編譯工具鏈的環境變量。
export PATH=/path/to/toolchains/arm-linux-gnueabihf/bin:$PATH
三、交叉編譯 openssl
1. 下載源碼
GitHub - openssl/openssl: TLS/SSL and crypto library
tar -xvzf openssl-openssl-3.5.0.tar.gz
2. 生成configure配置
setarch i386 ./config no-asm enable-shared --prefix=/path/to/openssl-openssl-3.5.0/arm32_install --cross-compile-prefix=arm-linux-gnueabihf- -v
解釋說明:
setarch i386
:聲明生成的是32位CPU,如果是64位CPU則去除該部分。enable-shared
:生成動態連接庫。no-asm
: 在交叉編譯過程中不使用匯編代碼,以加速編譯過程,且arm不支持匯編代碼。
輸出示例:
yoyo@yoyo:~/360Downloads/openssl-openssl-3.5.0$ setarch i386 ./config no-asm enable-shared --prefix=/path/to/openssl-openssl-3.5.0/arm32_install --cross-compile-prefix=arm-linux-gnueabihf- -v
C compiler: gcc
C compiler vendor: gnu
C compiler version: 1101
Configuring OpenSSL version 3.5.0 for target linux-x86
Using os-specific seed configuration
Created configdata.pm
Running configdata.pm
Created Makefile.in
Created Makefile
Created include/openssl/configuration.h**********************************************************************
*** ***
*** OpenSSL has been successfully configured ***
*** ***
*** If you encounter a problem while building, please open an ***
*** issue on GitHub <https://github.com/openssl/openssl/issues> ***
*** and include the output from the following command: ***
*** ***
*** perl configdata.pm --dump ***
*** ***
*** (If you are new to OpenSSL, you might want to consult the ***
*** 'Troubleshooting' section in the INSTALL.md file first) ***
*** ***
**********************************************************************
3. 修改Makefile
PLATFORM=arm
CROSS_COMPILE=arm-linux-gnueabihf-# 如果編譯出錯,則去掉 `-m32`
CNF_CFLAGS=-pthread # -m32
CNF_CXXFLAGS=-std=c++11 -pthread # -m32
注意:如果 Makefile 中存在 -m32
和 -m64
,則刪除。
4. 編譯安裝
make -j8
make install
編譯安裝之后的文件目錄:
yoyo@yoyo:~/360Downloads/openssl-openssl-3.5.0$ tree -L 2 arm32_install/
arm32_install/
├── bin
│ ├── c_rehash
│ └── openssl
├── include
│ └── openssl
├── lib
│ ├── cmake
│ ├── engines-3
│ ├── libcrypto.a
│ ├── libcrypto.so -> libcrypto.so.3
│ ├── libcrypto.so.3
│ ├── libssl.a
│ ├── libssl.so -> libssl.so.3
│ ├── libssl.so.3
│ ├── ossl-modules
│ └── pkgconfig
├── share
│ ├── doc
│ └── man
└── ssl├── certs├── ct_log_list.cnf├── ct_log_list.cnf.dist├── misc├── openssl.cnf├── openssl.cnf.dist└── private
5. 移植到開發板
# 拷貝 include
cp -r ./arm32_install/include/ /usr/local/openssl/# 拷貝lib
cp -r ./arm32_install/lib/ /usr/local/openssl/# 拷貝bin
cp -arf ./arm32_install/bin/ /usr/local/openssl/