RISC-V cross compiler has difficulty in including header files, such as cmath and mutex

Viewed 42

I'm using riscv gnu toolchain to cross compile C++ code for RISC-V, at an x86 host machine.

I have a simple code as an example, as follows.

#include <iostream>
#include <cmath>
#include <mutex>
int main(){
        float a = 1;
        float b = 2;
        std::cout << std::fmin(a, b) << std::endl;

        std::mutex _mu;
        return 0;
}

I compiled the code using the following command.

riscv64-unknown-elf-g++ test.cpp -o test.o -std=c++11 -I/workdir/riscv/install/riscv64-unknown-elf/include/c++/12.1.0 -I/workdir/riscv/install/riscv64-unknown-elf/include/c++/12.1.0/riscv64-unknown-elf -I/workdir/riscv/install/riscv64-unknown

But it produces errors about including cmath and mutex.

test.cpp: In function 'int main()':
test.cpp:7:27: error: 'fmin' is not a member of 'std'; did you mean 'min'?
    7 |         std::cout << std::fmin(a, b) << std::endl;
      |                           ^~~~
      |                           min
test.cpp:9:14: error: 'mutex' is not a member of 'std'
    9 |         std::mutex _mu;
      |              ^~~~~
test.cpp:4:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
    3 | #include <mutex>
  +++ |+#include <mutex>
    4 | int main(){

After searching google, I found that such classes and functions are defined in an if statement such as,

#if defined(_GLIBCXX_HAS_GTHREADS) ...

I received an answer from riscv gnu toolchain that it is out of scope of that repo.

Is there any approach to cross-compiling the C++ code that includes those header files?

I'm intending that the compiled code is run on spike simulator or qemu for RISC-V.

0 Answers
Related