std::mt19937 with ARM Compiler 6 uses sys_open and breaks retarget.c

I'm using Keil V5.30.0.0 and ArmClang.exe V6.14.

When I tried using std::mt19937, like so:

#include <random>

std::mt19937 mt;
std::uniform_int_distribution< uint32_t > dist(0);

auto a = dist( mt );

I got linker errors:

.\obj\rcu.axf: Error: L6200E: Symbol __stdin multiply defined (by stdio_streams.o and retarget.o).
.\obj\rcu.axf: Error: L6200E: Symbol __stdout multiply defined (by stdio_streams.o and retarget.o).
.\obj\rcu.axf: Error: L6200E: Symbol __stderr multiply defined (by stdio_streams.o and retarget.o).

I'm using more or less classical retarget.c, tuned for armclang.

I tried to remove those definitions from retarget.c and then got error:

.\obj\rcu.axf: Error: L6915E: Library reports error: __use_no_semihosting_swi was requested, but _sys_open was referenced

Then I removed asm(".global __use_no_semihosting_swi\n"); and my firmware hit BKPT before main, in call to _sys_open.

Why is this happening? mt19937 and uniform_int_distribution by themselves should not use any IO, should they? 

Parents
  • I suggest simplifying to a really simple test case, and working back up. I tried to build a simple test for Cortex-M4 based on your code fragment: no build errors. (Also MDK 5.30, ARMCLANG 6.14, configured to use STDIN with 'User' code) But my case was only built to test compilation, not to run anywhere.

    #include <random>
    #include <iostream>
    
    extern "C"{
    // Set RTE option Compiler::I/O::STDIN to 'User'
    int stdin_getchar (void);
    }
    
    std::mt19937 mt;
    std::uniform_int_distribution< uint32_t > dist(0);
    auto a = dist( mt );
    
    int main(void)
    {
       std::mt19937 mt;
       std::uniform_int_distribution< uint32_t > dist(0);
    
       auto b = dist( mt );
       //return a + b;
       std::cout << "Hello, world!\n" << a << std::endl;
       return 0;
    }
    

    The random number classes do define stream operator templates, so shouldn't be an issue unless you use stream operators with the distributions. I had no issues building with iostream or not. 

    I'm curious about your file retarget.c -- the RTE manager will add retarget_io.c to your project when you configure IO using the RTE manager. Why and how did your retarget.c need 'tuning' for ARMCLANG? 

    There is helpful documentation for retargeting I/O on the Keil website, here. Per that documentation, uVision doesn't support semihosting. This support article might also help.

Reply
  • I suggest simplifying to a really simple test case, and working back up. I tried to build a simple test for Cortex-M4 based on your code fragment: no build errors. (Also MDK 5.30, ARMCLANG 6.14, configured to use STDIN with 'User' code) But my case was only built to test compilation, not to run anywhere.

    #include <random>
    #include <iostream>
    
    extern "C"{
    // Set RTE option Compiler::I/O::STDIN to 'User'
    int stdin_getchar (void);
    }
    
    std::mt19937 mt;
    std::uniform_int_distribution< uint32_t > dist(0);
    auto a = dist( mt );
    
    int main(void)
    {
       std::mt19937 mt;
       std::uniform_int_distribution< uint32_t > dist(0);
    
       auto b = dist( mt );
       //return a + b;
       std::cout << "Hello, world!\n" << a << std::endl;
       return 0;
    }
    

    The random number classes do define stream operator templates, so shouldn't be an issue unless you use stream operators with the distributions. I had no issues building with iostream or not. 

    I'm curious about your file retarget.c -- the RTE manager will add retarget_io.c to your project when you configure IO using the RTE manager. Why and how did your retarget.c need 'tuning' for ARMCLANG? 

    There is helpful documentation for retargeting I/O on the Keil website, here. Per that documentation, uVision doesn't support semihosting. This support article might also help.

Children
More questions in this forum