Anyhow.. I sorta suspected it called the main.cpp on its own..
In C++ (and in C), the thing that "calls" the main() function is some run-time code that sets up stuff like the stack pointer and performs certain other CPU bookkeeping tasks that are necessary before user code can be run.
now my problem is to fix this..
In a "normal" C or C++ embedded program not running under an operating system, nothing ever returns from the main program. In a "normal" Arduino sketch, the sketch's loop()
function is called repeatedly in an infinite loop from the core library main()
function, and the sketch writer is not given the opportunity to return from main()
If you do make your own main()
function in a sketch and you return from that main()
, it is the same as executing the standard library exit()
function. (That's in the standard language specification document, and that's what happens with Arduino's avr-gcc and avr-libc.)
For the avr-gcc/avr-libc library that Arduino uses, the exit()
function causes interrupts to be disabled and the program enters a tight do-nothing loop. In Arduino, as in other embedded systems that run "barefoot," there is no operating system to return to.
Run-time initialization code and run-time library functions are part of the the avr-gcc library and are not part of the Arduino sketch or library. This is always linked into the execution code. Arduino-specific initialization code having to do with setting up timers for default operation is executed in the core library init()
function, which is called first in the Arduino core library main()
Bottom line: Thanks to the efforts of the Arduino development team you don't have to worry about a lot of these things. Look at Arduino examples. All of them have a setup()
function that is executed once and a loop()
function that is executed repeatedly inside the Arduino core library main()
function. A "normal" Arduino sketch must have these two functions. (See Footnote.)
After that, the sky's the limit.
It very well may be that one or the other (setup()
) is empty, but they both have to be there as long as you don't make your own main()
function in your sketch. (And if you decide to branch out with your own, non-Arduino, way, you may lose a fair amount of support in the generous and knowledgeable user community on this forum because most of us don't do it that way.)