I've been experimenting with Arduino on both esp8266 and AVR chips. The structure of the .ino file usually looks like this:

#include "Arduino.h"
#include "ESP8266WiFi.h"
##Some includes

void setup()
##Will be only run once at the begining.

void loop()
##Runs this code in a constant loop

My assumption is that somewhere behind simplification, before the compilation takes place, there is a full blown source file. It includes the platform specific initialization and contains the calls to setup() and loop(). When I say full blown source file I mean something that can be passed to the compiler and will compile given that the dependencies are met.

My questions are:

  • Is this assumption correct?
  • If so, can someone share an example on how does the full blown source file look like, and where did you get it from?

The Arduino core libraries (which are nothing more than a bunch of C/C++ files - there is no such thing as the "Arduino" programming language), are compiled along with your source code. These files include all of the initialisation routines and function definitions for things like digitalWrite and so forth.

There is a main.cpp file that gets compiled which is pretty much nothing more than:

#include "Arduino.h"

int main(void) {
    //Initialise timers and things (internal)
    //Your initialisation stuff (setup)
    //Loop forever
    for (;;) {
        //Calling your loop function

In terms of what your .ino file becomes, it is basically just copied to a temporary build directory and renamed to .cpp. There are some minor changes made, which is basically to just copy all of the function declarations and place them at the top of the file.

This process is actually why if you use your own header file which has some typedef in it which you then use in a function call, the code will break because the declarations get added above the #include statements meaning the custom type is used before it is defined.

If you go to the preferences you can tell it to enable verbosity during compilation. This will actually show you exactly what is being compiled. It will also give you the path to the temporary build directory in which you will find the .cpp file with the same name as your .ino file.

  • There are some minor changes made, which is basically to just copy all of the function declarations and place them at the top of the file. So the .ino file acts somewhat of a library. Makes sense. Will try to get to the main.cpp and expand your answer with the example after which I'll accept it. Jan 22 '17 at 14:23
  • Seems a little redundant to use "adruino" language, which then becomes C anyway. Jan 22 '17 at 18:14
  • 1
    @Serg despite what their website says, and the grand arguments about it, there is no such thing as the "Arduino" programming language, all it is is C++ with a little bit of badly conceived pre-processing. Jan 22 '17 at 18:28
  • 1
    @TomCarpenter I know, and that's exactly my point - why go to trouble of reinventing the wheel, when they could have just let people work in C/C++ directly ? Jan 22 '17 at 18:30

C++ (the actual language/compiler that is used) is a multiple-compilation-unit language. There is no single source file, but rather a bundle of compiled source files, that are linked together to create the executable (or downloadable image).

In you case the user sketch provides the setup() and loop() functions, which are called from another source file, which is provided by the Arduino IDE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.