無論在什麼平臺,OpenSSL的編譯一般需要經過三個步驟:
使用Configure批處理,根據平臺環境、選用的編譯器及其他參數設置編譯的環境變量。
通過使用批處理指令來根據上面設置好的環境變量生成編譯腳本文件。
使用特定的編譯指令進行編譯鏈接。
本文將介紹OpenSSL在Linux、Windows、Android、Mac和iOS平臺的編譯方式。
一、OpenSSL在Linux平臺的編譯
打包環境
需要安裝perl、make和gcc。
源碼準備
編譯安裝
執行config
./config --prefix=/home/workspace/openssl/output/64
./config -t
執行make
make
執行結果:
執行make test
make test
執行結果:
執行make install
make install
執行完成後,編譯生成的文件將會放到/home/workspace/openssl/output/64目錄下:
二、OpenSSL在Windows平臺的編譯
打包環境
編譯工具:Visual Studio 2019
INSTALL文件說明了OpenSSL在Windows編譯的方式:
NOTES.WIN文件說明了編譯需要的額外依賴工具,我們需要下載安裝這些工具,並配置環境變量:
編譯64位庫
打開[x64 Native Tools Command Prompt for VS 2019]:【開始】-【所有程序】—【Microsoft visual studio2019】—【x64 Native Tools Command Prompt for VS 2019】(以管理員身份運行)
切換到OpenSSl源碼目錄
執行編譯
#第一步
#若需要編譯靜態庫需要增加no-shared參數,否則默認生成動態庫
#perl Configure VC-WIN64A no-shared --prefix=C:\Users\Username\Desktop\openssl\output\64
perl Configure VC-WIN64A --prefix=C:\Users\Username\Desktop\openssl\output\64
#第二步
nmake
#第三步
nmake test
#第四步
nmake install
編譯完成的目標文件:
錯誤處理
注意:若在執行perl Configure VC-WIN32 --prefix=C:\Users\Username\Desktop\openssl\output\32或perl Configure VC-WIN64A --prefix=C:\Users\Username\Desktop\openssl\output\64時出現下列錯誤
It looks like you don't have either nmake.exe or dmake.exe on your PATH,
so you will not be able to execute the commands from a Makefile. You can
install dmake.exe with the Perl Package Manager by running:
ppm install dmake
執行
ppm install dmake
若執行ppm install dmake還出現下面的錯誤:
ppm install failed: DBD::SQLite::db selectrow_array failed: database disk image is malformed
刪除C:\Users\FengGuodong\AppData\Local\ActiveState\ActivePerl目錄下的文件:
重新執行
ppm install dmake
等待下載安裝完成
dmake安裝完成後,重新開始編譯:
#第一步
#編譯32位庫
#若需要編譯靜態庫需要增加no-shared參數,否則默認生成動態庫
#perl Configure VC-WIN32 no-shared --prefix=C:\Users\Username\Desktop\openssl\output\32
perl Configure VC-WIN32 --prefix=C:\Users\Username\Desktop\openssl\output\32
#編譯64位庫
#若需要編譯靜態庫需要增加no-shared參數,否則默認生成動態庫
#perl Configure VC-WIN64A no-shared --prefix=C:\Users\Username\Desktop\openssl\output\64
#perl Configure VC-WIN64A --prefix=C:\Users\Username\Desktop\openssl\output\64
#第二步
nmake
#第三步
nmake test
#第四步
nmake install