Question: what would cause the c/c++ lib run time init fails?

I'v been trying to use the c++'s cout object with my stm32 (cotex-m3) board.

I think I'v reimplemented the necessary functions, after reading the related topics of the documentation, such as < http://www.keil.com/support/man/docs/armlib/armlib_CJAIABCF.htm Redefining low-level library functions to enable direct use of high-level library functions in the C library > and < http://www.keil.com/support/man/docs/armlib/armlib_cjajiegc.htm Using the libraries in a nonsemihosting environment > and so on.

The code I wrote is just like this,

#pragma import(__use_no_semihosting)

#include <stdio.h>
namespace std{
 struct __FILE
  {
    int handle;

    /* Whatever you require here. If the only file you are using is */
    /* standard output using printf() for debugging, no file handling */
    /* is required. */
  };

  FILE __stdout;
  FILE __stdin;
  FILE __stderr;

  int fputc(int ch, std::FILE *f)
  {
    /* ...
        I put the char to the serial port
    */
    return 0;
  };

   ...
}

#include <stdio.h>
#include <iostream>
using std::cout;
using std::endl;
int main(void) {

        printf("printf: hello world!\n");

        cout<<"hello world"<<endl;

            ...
}

I've done the necessary hardware initialization in the startup code before the library init start ( call __main ).

Program work pretty well when using the software simulation. It works too when I download the code to my mcu board, only sometimes! It fails most of the times! what is more wierd is, when the mcu output char strings successfully, I turn its power off, and then turn it on, the program fails again!

I've notice that the program is abort during the program initialization (after __main and before main is called). I then re-implemented the abort() function and output "abort" in it only to found that abort is called by the lib during init.

The most distasting thing is that the library dosen't output any imformation about the error, even though I've re-implemented the _ttywrch(int ch) function!

So my question is, what may cause the c/c++ lib run time init failure ? Do I miss any step to use the std::cout in an nonsemihosting environment?

ps: I'm quite sure the heap memory is enough, I define the Heap_Size as 0x1000. It was defined 0x400 before and the program output "SIGABRT: Abnormal termination"(I don't quite get it. Why not output information such as "out of memory" ? Who wouldn't know the program has terminated without the "SIGABRT: Abnormal termination" ???).

Parents
  • Problem solved.
    I uncheck the NoInit option for the RAM, and then the program went normally.

    The simulator clear the ram before the program start, so the program goes pretty well.

    While using real hardware, the content of RAM is unknown after the mcu's being powered up.

    I think this may be considered a bug of the library, because it cannot successfully be initialized with the "NoInit" option, which is used to tell the compiler to leave the undefined-init-value global variables alone. What's worse, the library didn't even report any useful debug information and that really make me having a hard time debuggin the program!

Reply
  • Problem solved.
    I uncheck the NoInit option for the RAM, and then the program went normally.

    The simulator clear the ram before the program start, so the program goes pretty well.

    While using real hardware, the content of RAM is unknown after the mcu's being powered up.

    I think this may be considered a bug of the library, because it cannot successfully be initialized with the "NoInit" option, which is used to tell the compiler to leave the undefined-init-value global variables alone. What's worse, the library didn't even report any useful debug information and that really make me having a hard time debuggin the program!

Children
More questions in this forum