编译 wolfssl 库

wolfssl github: https://github.com/wolfSSL/wolfssl


  • 编译 .lib 或者 .dll

wolfssl 很好的提供了 win32 的工程=》sln 文件

这样就不用折腾 CMakeLists 文件了,使用 Visual Studio 打开 sln 文件后,设置好 Static 编译库即可,开箱即用


  • 编译 .a 文件

如果需要编译 .a 库,可以借助 MINGW64 终端

因为 wolfssl 已经提供了 CMakeLists.txt,所以编译起来还是省事不少

在 MINGW64 终端中 cd 到 wolfssl/src,执行以下命令行:

cmake -S . -B build -G Ninja -DCMAKE_FLAGS="-DWOLFSSL_CRYPT_ONLY:BOOL=ON -DWOLFSSL_CRYPT_TESTS:BOOL=OFF -DWOLFSSL_DSA:BOOL=ON -DWOLFSSL_ECC:BOOL=OFF -DWOLFSSL_RSA:BOOL=OFF -DWOLFSSL_FILESYSTEM:BOOL=OFF -DWOLFSSL_ASYNC_THREADS:BOOL=OFF -DWOLFSSL_OLD_NAMES:BOOL=OFF -DBUILD_SHARED_LIBS:BOOL=OFF"

接着 ninja,可能会提示缺少 <stdint.h>

可以在源码中添加以下代码,

#if defined(__MINGW64__)
#include <stdint.h>
#endif

这样重新编译后就成功了

不过这样编译出的库名字有点奇葩,会编译出 libwolfssl.dll.a 的库

故可以采用 sh 脚本方法一步到位,也不会报缺少头文件的错误

build_mingw64.sh

#!/bin/bash
set -e

CMAKE_FLAGS="-DWOLFSSL_CRYPT_ONLY:BOOL=ON -DWOLFSSL_CRYPT_TESTS:BOOL=OFF -DWOLFSSL_DSA:BOOL=ON -DWOLFSSL_ECC:BOOL=OFF -DWOLFSSL_RSA:BOOL=OFF -DWOLFSSL_FILESYSTEM:BOOL=OFF -DWOLFSSL_ASYNC_THREADS:BOOL=OFF -DWOLFSSL_OLD_NAMES:BOOL=OFF -DBUILD_SHARED_LIBS:BOOL=OFF"

mkdir -p package/ming64/include
mkdir -p package/ming64/lib

rm -rf build

cmake -S . -B build -G Ninja $CMAKE_FLAGS
cd build
ninja
cp -r wolfssl ../package/ming64/include/
cp -r libwolfssl.a ../package/ming64/lib/
cd ..

rm -rf build

另附鸿蒙库的编译脚本:

#!/bin/bash
set -e

NDK_PATH=~/Library/OpenHarmony/sdk/native/4.0
CMAKE=~/Library/OpenHarmony/sdk/native/4.0/build-tools/cmake/bin/cmake

CMAKE_FLAGS="-DWOLFSSL_CRYPT_ONLY:BOOL=ON -DWOLFSSL_CRYPT_TESTS:BOOL=OFF -DWOLFSSL_DSA:BOOL=ON \
             -DWOLFSSL_ECC:BOOL=OFF -DWOLFSSL_RSA:BOOL=OFF -DWOLFSSL_FILESYSTEM:BOOL=OFF \
             -DWOLFSSL_ASYNC_THREADS:BOOL=OFF -DWOLFSSL_OLD_NAMES:BOOL=OFF -DBUILD_SHARED_LIBS:BOOL=OFF"

arch=arm64-v8a

rm -rf build

 $CMAKE -B build -G Ninja -DOHOS_STL=none -DOHOS_PLATFORM=OHOS \
   -DCMAKE_TOOLCHAIN_FILE="${NDK_PATH}/build/cmake/ohos.toolchain.cmake" \
   -DCMAKE_C_FLAGS="-Qunused-arguments" $CMAKE_FLAGS -DOHOS_ARCH=$arch
cd build
ninja

mkdir -p ../package/ohos/include/$arch
cp -r wolfssl ../package/ohos/include/$arch/
mkdir -p ../package/ohos/lib/$arch
mv libwolfssl.a ../package/ohos/lib/$arch/

cd ..