WASI是一个模块化的系统界面,旨在为任何符合WASI的运行时构建可运行的.wasm模块,而不仅仅是为Node.js和Web浏览器。尽管WASI仍在开发中并且尚未稳定,但是它可用于某些实验项目。在本文中,我将分享如何通过移植ZXing C ++使用WASI SDK来构建.wasm条码读取器模块。
安装WASI运行时
Wasmer和Wasmtime都支持WASI。
Wasmercurl https://get.wasmer.io -sSfL | sh
Wasmtime
获取源代码并使用Rust编译器构建wasmtime 。
设置WASI建筑环境
让我们开始使用wasi-libc。
要构建WASI Libc,您必须使用clang 8或更高版本。如果以前没有安装clang,则可能会发现apt软件存储库中未列出最新的clang版本。
解决方法是将“deb http://deb.debian.org/debian/testing main” 添加到“/etc/apt/sources.list”并更新源:sudo apt update
然后可以看到最新的clang 9:
安装clang 9并创建相关的符号链接:sudo apt install clang-9
sudo ln -s /usr/bin/clang-9 /usr/bin/clang
sudo ln -s /usr/bin/clang++-9 /usr/bin/clang++
现在,我们可以从源代码构建WASI库。
如果您只想使用工具链,则更方便的方法是安装wasi-sdk:sudo dpkg -i wasi-sdk_7.0_amd64.deb
export PATH=/opt/wasi-sdk/bin:$PATH
export CC=/opt/wasi-sdk/bin/clang
export CXX=/opt/wasi-sdk/bin/clang++
为测试创建一个“hello world”程序:#include
int main()
{
printf("hello wasi libc\n");
return 0;
}
构建代码:$ clang - target=wasm32-wasi - sysroot=/opt/wasi-sdk/share/wasi-sysroot/ test.c -o test.wasm
运行应用程序:wasmer run test.wasm
wasmtime test.wasm
移植ZXing C ++ for WASI SDK
获取zxing-cpp 的源代码。
由于当前的WASI libc尚不支持C ++异常,因此我们需要在CMakeLists.txt中添加-fno-exceptions:set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} - target=wasm32-wasi -Wall -Wextra -fno-exceptions")
另外,将sysroot设置如下:set (CMAKE_SYSROOT /opt/wasi-sdk/share/wasi-sysroot)
为了通过构建,我禁用了所有与C ++异常相关的代码,并调整了项目结构。
将项目构建为wasm文件:mkdir build
cd build
cmake ..
cmake --build .
在构建文件夹下运行该应用程序:
Text: MEBKM:URL:http\://en.wikipedia.org/wiki/Main_Page;;
Format: QR_CODE
Position: 190x367 205x162 422x165 405x342
EC Level: M
$ wasmtime zxing_barcode_reader.wasm - dir=$(pwd)/../ $(pwd)/../test.png
Text: MEBKM:URL:http\://en.wikipedia.org/wiki/Main_Page;;
Format: QR_CODE
Position: 190x367 205x162 422x165 405x342
EC Level: M
如何使用Wapm发布和运行Wasm文件
使用init命令生成wapm.toml文件:$ wapm init zxing_barcode_reader
编辑wapm.toml:[package]
name = "yushulx/zxing_barcode_reader"
version = "0.1.4"
description = "A barcode reader app built with ZXing C/C++ and wasi-sdk"
readme = "README.md"
repository = "https://github.com/yushulx/wasi-zxing-wasm"
[[module]]
name = "zxing_barcode_reader"
source = "dist/zxing_barcode_reader.wasm"
abi = "wasi"
[[command]]
name = "zxing_barcode_reader"
module = "zxing_barcode_reader"
将程序包发布到wapm.io:$ wapm login
$ wapm publish
通过wapm安装软件包,并从PNG图像中读取条形码:$ wapm install yushulx/zxing_barcode_reader
$ wapm run zxing_barcode_reader --dir=. test.png
本文内容就是这样了,希望对您有所帮助!您可以关注我们查看更多相关资讯,或者点击下载Dynamsoft Barcode Reader正式版体验一下~
想要购买该产品正版授权,或了解更多产品信息请点击“咨询在线客服”
标签:
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至hey@evget.com