C++ programs on the Arduino platform do not have a main() function like on other platforms; instead, it must have two functions named setup() and loop(). But according to C++ standard - all C++ programs must have a main() function.
So, does such C++ code conform to the standard? Do C++ compilers/linkers/loaders on Arduino conform to the standard?
The C++ standard provides for two execution environments: freestanding and hosted. Most folks here run in a hosted environment, where your program starts in
main(). Embedded systems run in a freestanding environment, where program startup is through an implementation-defined mechanism. Compilers for a freestanding environment are allowed to leave out some parts of the standard library. For more details, see here.So,
setup()andloop()are okay in a freestanding environment. Nomain()required. I don’t know if the library for Arduino meets the requirements in the standard.In a hosted environment, there’s typically an operating system (the host) that lets you launch programs. C++ programs for such an environment must have
main(). In a freestanding environment, the program typically starts when the device is turned on. That’s much closer to the metal, and the system is allowed to have its own requirements, in order simplify the boilerplate code that fires off the application.