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" ???).