如何修复 PlatformIO 中 mbed printf() 打印字面量 %f 的问题

问题:

你正在使用类似这样的代码

example.cpp
printf("%f\n", myFloat);

在你的 mbed/PlatformIO 应用程序中,但它不是打印 myFloat 而是打印字面量 %f

解决方案

mbed 默认使用 minimal-printf 库,该库配置为在微控制器上节省空间。因此,浮点支持(即 %f 支持)默认禁用。你需要通过在 PlatformIO 项目的根目录中添加 mbed_app.json 并包含 "platform.minimal-printf-enable-floating-point": true 来启用它:

mbed_app.json
{
    "target_overrides": {
      "*": {
        "platform.minimal-printf-enable-floating-point": true
      }
    }
}

有关更多详细信息和类似选项,请参见平台配置选项页面。


Check out similar posts by category: C/C++, Mbed, PlatformIO