【问题标题】:std::cout printing nan or zeros for float or double variables in armv7lstd::cout 在 armv7l 中为浮点或双精度变量打印 nan 或零
【发布时间】:2021-10-27 11:27:57
【问题描述】:

我正在为 Raspberry PI 3B+ 编写一个 C++ 程序,使用 conan.io Docker 容器 conanio/gcc7-armv7 for cross compilation 进行交叉编译。在我开始打印双精度值并在输出中得到“nan”或错误值之前,一切都运行良好。我将代码简化为显示问题的最小示例:

#include <cstdio>
#include <iomanip>
#include <iostream>

int main() {
    double foo = 1.234567;
    float bar = 89.10121314;
    std::cout << std::fixed << std::setw(4) << std::setprecision(3) << "foo " << foo << '\n';
    std::printf("foo %4.2f\n", foo);
    std::cout << std::fixed << std::setw(4) << std::setprecision(3) << "bar " << bar << '\n';
    std::printf("bar %4.2f\n", bar);
}

我正在运行 Ubuntu 20.04 的 x86_64 笔记本电脑上构建。当我使用 gcc 9.3.0 为 x86_64 构建并在我的笔记本电脑上运行它时,我得到了预期的输出:

foo 1.235
foo 1.23
bar 89.101
bar 89.10

但是当我在我的 Raspberry PI 中交叉编译并运行它时,std::cout 会打印 -nan:

foo -nan
foo 1.23
bar -nan
bar 89.10

std::cout 不起作用,但 printf 工作正常。知道这里发生了什么吗?

谢谢

【问题讨论】:

  • 有趣的问题,问得好。我想知道:是否可以尝试除 gcc7 之外的其他工具链,例如更新的 gcc 或 clang?
  • 听起来像是浮点 ABI 的问题,我似乎记得 Raspberry Pi 有点特别,因为它与 ARMv6 与硬浮点兼容。

标签: c++ raspberry-pi arm cross-compiling


【解决方案1】:

这通过将我用于交叉编译的 Docker 映像从 conanio/gcc7-armv7 更改为 conanio/gcc10-armv7hf 来解决。除了gcc版本(7.2.0 vs 10.3.0),它们之间的主要区别在于~/.conan/profiles/default,前者使用arch=armv7,后者使用arch=armv7hf,根据dpkg --print-architecture,我应该是用于 Raspberry PI B+。

感谢@ted-lyngmo 提示尝试使用另一个编译器版本,感谢@eof 提示考虑浮点设置

【讨论】:

    猜你喜欢
    • 2010-10-08
    • 2011-11-17
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多