4

You know , a simple arduino sketch has no main function , no include preprocess cmds,so I just want to know how arduino IDE convert a sketch to normal C/C++ source code,and what is the source code looks like.

If possible,I also want to use cmd like g++ -E to see the .i file genetated after preprocess.How to do it?

ps: my English is poor.

6

You can turn on verbose compiling.
This will show all the compiler commands used by Arduino IDE.

The ino file is converted to a .cpp file (which you can see in the verbose output). Basically the conversion is as follows:

  1. merge all .ino files into one big .ino file with the [project].ino file first
  2. Add arduino.h
  3. Add all function declarations of methods defined in the .ino files
  4. Save as .cpp file

As to the lack of main method. The core contains a main.cpp file which defines main in avr world this looks like

int main(void)
{
    init();

    initVariant();

#if defined(USBCON)
    USBDevice.attach();
#endif

    setup();

    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }

    return 0;
}
  • yes,I get it! I post a brief artical abouts this.link thx :) – user7028945 Apr 1 '17 at 15:27
5

I'm not entirely sure what the arduino-builder does, but it should be similar to the traditional method.

Given the "bare minimum" sketch of:

void setup() {
}

void loop() {
}

First the IDE combines all .ino and .pde files together into one large one. I believe the order of concatenation is:

  1. Start with the "main" INO file (the one named the same as the folder it's in).
  2. Append to that each other .ino or .pde file in alphabetical order.

Then the IDE will add the required #include <Arduino.h> and function prototypes for any functions found - so it would end up as:

#include <Arduino.h>

void setup();
void loop();

void setup() {
}

void loop() {
}

Finally it runs normal gcc and g++ commands on the generated file(s). You can find the generated file(s) in the build folder. Where that is depends on the operating system you are using. Turn on verbose compilation in the preferences and you can see the gcc and g++ commands that are being executed, and as well the paths for the files involved, which lets you then see where the preprocessed files are stored.

Another important step the IDE performs, which doesn't modify the files themselves, is to look for any #include entries that match libraries and add -I <path> to the compilation commands for those libraries. It also then finds any source files associated with those libraries and compiles them. It also compiles all the core source files, which is where the main() functions is (see @jantje's answer for details on how the main() function is formed).

Once the IDE has finished with its parsing and processing it's all standard gcc and g++.

  • you beat me ;-) – jantje Mar 30 '17 at 10:51
  • your answer is helpful! thx! :) – user7028945 Apr 1 '17 at 15:27

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.