What does the IDE change in my sketch



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.

Tags: Arduino, IDE
Last update:
2014-03-02 01:54
Author:
Christopher Andrews
Revision:
1.5
Average rating: 4 (2 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.