Search code examples
c++raspberry-piarmcross-compiling

std::cout printing nan or zeros for float or double variables in armv7l


I'm writing a C++ program for a Raspberry PI 3B+ doing cross compilation using conan.io Docker container conanio/gcc7-armv7 for cross compilation. Everything was working fine until I started printing double values, and getting "nan" or wrong values in the output. I simplified my code to this minimal example that shows the problem:

#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);
}

I'm building on a x86_64 laptop running Ubuntu 20.04. When I build for x86_64 with gcc 9.3.0 and run it on my laptop, then I get the expected output:

foo 1.235
foo 1.23
bar 89.101
bar 89.10

But when I cross compile and run it in my Raspberry PI then std::cout prints -nan:

foo -nan
foo 1.23
bar -nan
bar 89.10

std::cout doesn't work but printf works fine. Any idea was it's going on here?

Thanks


Solution

  • This solved by changing the Docker image I'm using for cross compilation from conanio/gcc7-armv7 to conanio/gcc10-armv7hf. Besides the gcc version (7.2.0 vs 10.3.0), the main difference between them is in ~/.conan/profiles/default, the former uses arch=armv7 while the latter uses arch=armv7hf, which according to dpkg --print-architecture is what I should be using for a Raspberry PI B+.

    Thanks @ted-lyngmo for the hint to try with another compiler version, and @eof for the hint to take floating point settings into account