You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PlatformIO为Arduino项目单CPP文件构建本地测试?

解决PlatformIO本地测试中main函数未定义的问题

问题出在你当前的native_test环境仅指定编译nmea.cpp,但测试代码中的main函数未被纳入构建范围,导致链接阶段找不到程序入口。

以下是正确的配置方案:

  • 为native环境添加framework = none,本地测试无需依赖Arduino框架
  • 修改build_src_filter,同时包含目标源文件和测试代码文件(通常测试代码放在test目录下)

修改后的platformio.ini配置如下:

[env:mkrwifi1010]
platform = atmelsam
board = mkrwifi1010
framework = arduino
lib_deps = 
    arduino-libraries/LiquidCrystal@^1.0.7
    ivanseidel/LinkedList@0.0.0-alpha+sha.dac3874d28
    arduino-libraries/WiFiNINA@^1.8.13
    bblanchon/ArduinoJson@^6.21.1

[env:native_test]
platform = native
framework = none
build_src_filter = +<src/nmea.cpp> +<test/**/*>

如果你的测试代码不在test目录下,将+<test/**/*>替换为实际的测试文件路径即可。这样配置后,PlatformIO会同时编译nmea.cpp和测试代码,链接时就能找到main函数完成构建。

内容的提问来源于stack exchange,提问作者Bart Friederichs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:00:02