The IDE makes subtle changes to the code when you compile your sketch. The changes are not permenant as a copy of your sketch is used to do the compiling. This is a consequence of the IDE allowing you to compile the sketch without saving into a file, which is what the compiler requires.
In a breif rundown, the IDE does three major activities.
- Adds an include file for the Arduino core
This process is a fairly harmless addition, if users include Arduino.h explicitly, the header has
#ifndef
guards protecting it from multiple declaration errors which is typical when including an unprotected header into multiple locations. - Generates function prototypes
This is also a generally unobtrusive step. As with protected include files, you can have multiple declarations of a function, it is the definitions which must be unique.
- Collects included libraries
As the sketch is compiled in a temporary location, the IDE needs to ensure all include paths are valid. To accomplish this the IDE scans the sketch for all headers that match files residing in the libraries folder, then it simply copies the library source files into the temporary location along with the sketch.
This has been a source of error for many new Arduino users attemping to write their own libraries. As confusing as it sounds, any library included into other libraries must also be included in the sketch; simply so it can be copied to the temporary location.
To show a quick example of what the modifications look like; here is the BareMinimum sketch, and its modified result.
- Before
void setup(){ } void loop(){ }
- After
#line 1 "BareMinimum.ino" #include "Arduino.h" void setup(); void loop(); #line 2 void setup(){ } void loop(){ }
The modifications performed are great for beginners that do not know the C++ fundamentals required for building simple sketchs. The sketch looks tidier to the novice with only a setup()
/loop()
implementation cluttering up the workspace. However once you start using certain C++ features you can run into some common pitfalls as a result of these modifications.
Visit the FAQ entries below for an explanation of some known issues with the automated IDE code modifcations.
- Why do #ifdef macro's stop the sketch from compiling
The IDE has been known to cause errors with certain uses of
#ifdef
. - My class/reference won't work in the sketch
Classes and structures directly in the sketch file can have problems when used as the parameter to functions.
- My custom library won't work, It says: X was not declared in this scope / The library is in the right folder but it does not work
These articles explain a few small issues with installing and using libraries.