在树莓派通过PlatformIO运行wasm3时出现链接错误求助
解决PlatformIO编译wasm3时数学函数未定义的链接错误
在Raspberry Pi 3 Model B(Raspbian系统)上用VS Code的PlatformIO扩展开发C+wasm3代码时,链接阶段出现sqrt、rint、ceil等数学函数未定义的错误,尝试过在platformio.ini中添加build_flags = -lm或lib_ldf_mode = chain+但问题依旧,具体错误日志:
Verbose mode can be enabled via `-v, --verbose` option CONFIGURATION: https://docs.platformio.org/page/boards/linux_arm/raspberrypi_3b.html PLATFORM: Linux ARM (1.6.0) > Raspberry Pi 3 Model B HARDWARE: BCM2837 1200MHz, 1GB RAM, 1GB Flash PACKAGES: - framework-wiringpi @ 1.242.0 (2.42) LDF: Library Dependency LDF Modes: Finder ~ chain, Compatibility ~ soft Found 1 compatible libraries Scanning dependencies... Dependency Graph |-- Wasm3 @ 0.5.0 Building in debug mode Linking .pio/build/raspberrypi_3b/program /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Sqrt_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:272: undefined reference to `sqrt' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Sqrt_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:272: undefined reference to `sqrt' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Nearest_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:273: undefined reference to `rint' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Nearest_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:273: undefined reference to `rint' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Trunc_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:271: undefined reference to `trunc' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Trunc_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:271: undefined reference to `trunc' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Floor_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:270: undefined reference to `floor' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Floor_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:270: undefined reference to `floor' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Ceil_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:269: undefined reference to `ceil' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f64_Ceil_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:269: undefined reference to `ceil' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Sqrt_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:272: undefined reference to `sqrtf' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Sqrt_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:272: undefined reference to `sqrtf' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Nearest_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:273: undefined reference to `rintf' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Nearest_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:273: undefined reference to `rintf' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Trunc_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:271: undefined reference to `truncf' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Trunc_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:271: undefined reference to `truncf' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Floor_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:270: undefined reference to `floorf' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Floor_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:270: undefined reference to `floorf' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Ceil_s': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:269: undefined reference to `ceilf' /usr/bin/ld: .pio/build/raspberrypi_3b/lib47f/Wasm3/m3_compile.o: in function `op_f32_Ceil_r': /home/admin/Dokumente/PlatformIO/Projects/WebAssembly/.pio/libdeps/raspberrypi_3b/Wasm3/src/m3_exec.h:269: undefined reference to `ceilf' collect2: error: ld returned 1 exit status *** [.pio/build/raspberrypi_3b/program] Error 1 ====================== [FAILED] Took 4.48 seconds ======================
解决方案
1. 强制链接数学库,修正链接顺序
修改platformio.ini,给build_flags添加-Wl,--no-as-needed参数,确保libm不会被链接器忽略:
[env:raspberrypi_3b] platform = linux_arm board = raspberrypi_3b framework = wiringpi build_flags = -lm -Wl,--no-as-needed lib_ldf_mode = chain+
原理:默认情况下链接器会跳过未被当前代码直接引用的库,但wasm3库内部调用了数学函数,--no-as-needed强制链接器包含libm,不管主代码有没有直接用。
2. 直接指定LINKFLAGS
如果上面的方法不行,尝试直接设置链接器标志,确保-lm被添加到链接命令的末尾:
[env:raspberrypi_3b] platform = linux_arm board = raspberrypi_3b framework = wiringpi lib_ldf_mode = chain+ LINKFLAGS = -lm
3. 启用wasm3的数学函数宏
部分wasm3版本需要定义宏来启用数学函数支持,添加-DM3_USE_MATH=1到build_flags:
build_flags = -lm -DM3_USE_MATH=1
4. 临时验证:手动补全链接命令
如果以上都无效,可以手动修改PlatformIO生成的链接命令,在最后加上-lm。操作步骤:
- 打开PlatformIO的终端,运行
pio run -v查看完整的链接命令 - 复制该命令,在末尾添加
-lm后执行,验证是否能成功链接
内容的提问来源于stack exchange,提问作者Lukas_1995
相关产品推荐
相关产品推荐

