I try the code as stated below. The listing 1 works correctly but listing 2 only prints he
and doesn’t show anything else. It shows that whether the code works or not is depending on where I place the declaration. It makes me very concerned. Anyone know what is going on?
I have a C background only. I remember I encountered something similar a year ago and I decided not trying anymore cpp on mcu
// /Listing 1
#include <stdio.h>
#include "mbed.h"
using namespace mbed;
int main() { //
while(1){
osDelay(1000);
printf("hello\r\n");
}
}
UARTSerial console(USBTX, USBRX);
FileHandle *mbed::mbed_override_console(int) {
// static UARTSerial console(USBTX, USBRX);
// UARTSerial* console = new UARTSerial(USBTX, USBRX);
return &console;
}
/// Listing 2
#include <stdio.h>
#include "mbed.h"
using namespace mbed;
int main() { //
while(1){
osDelay(1000);
printf("hello\r\n");
}
}
// UARTSerial console(USBTX, USBRX);
FileHandle *mbed::mbed_override_console(int) {
static UARTSerial console(USBTX, USBRX);
// UARTSerial* console = new UARTSerial(USBTX, USBRX);
return &console;
}
Tools:
- mbed-cli==1.8.3
- mbed-os, fork from 5.10. I made some modifications by deleting unused features library and add my own board target.
- armcc compiler 5