6

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?

ocrdu
  • 2,172
  • 6
  • 15
  • 22
christo
  • 381
  • 2
  • 5

2 Answers2

7

The C++ standard provides for two execution environments: freestanding and hosted. Most folks here run in a hosted environment, where your program starts inmain(). 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() and loop() are okay in a freestanding environment. No main() 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.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • 3
    Not worth me adding a new answer with the standard text if you want to include it in this: https://timsong-cpp.github.io/cppwp/basic.start.main#1.sentence-3 – NathanOliver Oct 28 '20 at 21:29
  • 2
    It provides no std. You can't even `new` an object. There are partial implementations available which you import by adding to your application source code. – François Andrieux Oct 28 '20 at 21:39
  • 1
    Added a link to the standard text as well, hope that's ok. – cigien Oct 28 '20 at 21:52
  • 1
    @cigien — I rolled back your addition. That’s a small part of the standard”s wording about freestanding environments. It addresses `main()`, but not the library requirements. – Pete Becker Oct 28 '20 at 22:03
  • 1
    That's fair. I still feel that the standard text about `main`, and the text about the library requirements should be added to the answer. Just linking to cppreference for a language-lawyer question doesn't feel right to me :p – cigien Oct 28 '20 at 22:06
  • 1
    @cigien — I don’t think it’s a language lawyer question. It’s more a question of basic knowledge. As the comments to the question suggest, most people aren’t aware of the existence of freestanding environments. – Pete Becker Oct 28 '20 at 22:23
  • 1
    Hmm, I'm not sure I agree with that reasoning. The question says, "does this code conform to the standard?" That alone qualifies it, I think. And the fact that people don't seem to have some basic knowledge is neither here nor there. Not to mention that what you consider basic knowledge might be an advanced concept for others ;) – cigien Oct 28 '20 at 22:27
  • @FrançoisAndrieux you can't `new` an object? That's not true at all. Am I missing something? – rfb Jan 25 '23 at 10:52
  • @rfb The last time I used the default Arduino C++ environment, you had to provide operator new yourself to get a new expression working. You couldn't `new` out-of-the-box. – François Andrieux Jan 25 '23 at 14:20
  • @FrançoisAndrieux OK fine. But this didn't apply to the versions at the date of your answer, forgive me. – rfb Jan 26 '23 at 16:48
5

Just to have said it out loud here: there is a main.cpp, that looks roughly like this:

#include <Arduino.h>

int main(void) {
  init();
  setup();
  for (;;) {
    loop();
  }
  return 0;
}

The Arduino IDE supplies it. The IDE also generates function prototypes if they aren't there already, and does some other things.

So, the absence of a main() in the visible code in the Arduino IDE doesn't mean it isn't there, or that it violates the standard.

ocrdu
  • 2,172
  • 6
  • 15
  • 22