Anyone who has worked with Arduino has stuffed things in the global namespace before. To anyone well versed in programming, this is a known bad practice, but it's convenient and it sometimes works so people do it anyhow. It can be responsible for a lot of mysterious bugs that are even harder to track down without the Serial port, which idiomatically gets set up in setup(), after constructors called for an globally visible objects have been called. The hack where you set up Serial in a static constructor also isn't advisable as the order of setup for things defined outside of functions is not guaranteed.
// Constructor gets called before setup(), so anything logged internally is lost.
// DON'T DO THE FOLLOWING LINE!
// MyObject mySharedObject(/*args_if_any*/);
struct Globals {
Globals()
: mySharedObject(/* args_if_any*/) {}
// Any mutable data that should persist across runtime should live in here.
// Do do this.
MyObject mySharedObject;
};
void setup() {
Serial.begin(115200); // Or whatever rate...
}
// You can setup more things inside shared here if you want. Note that the & is
// really important so you aren't making copies and doing actions that are lost at the
// end of the function call.
void MySetup(Globals& shared) {...}.
void MyLoop(Globals& shared) {...}
void loop() {
Globals shared;
// Your own defined setup function.
MySetup(shared);
while(true) {
// Your own defined loop function.
MyLoop(shared);
}
}