Multiple Source Files with the Arduino IDE

Granted, the Arduino IDE isn’t conducive to heavy-duty projects, but it’s useful for quick and dirty programs.

Once you outgrow a single file and wish to break the project up into multiple files, you can do so in the Arduino IDE with multiple tabs. Each tab represents a single file stored in the same directory as the sketch file (.ino).

You create new tabs using the drop-down menu that appears by clicking on the inverted triangle above the upper right-hand corner of the editor.

Showing the Tab Menu in the Arduino 1.0.1 IDE

Note that unlike the sketch’s main .ino file, you will not have automatic access to the Arduino basic definitions. That is easily remedied by including

#include "Arduino.h"

Happy Hacking.

Library Warning

Please note that there is a quirk in the Arduino IDE that can prevent libraries from being included in the compile. The IDE requires all libraries to be #include’d in the main (.ino) file. If you do not do this, the library will not be included, and you’ll get errors during compile.

How It Works

When compiling, the IDE copies all the files to a temporary directory. It scans the .ino file for libraries, and copies the library files to the temporary directory. Finally, it does the actual compile. If you #include a library in any of the tabbed files, but not in the .ino file, the library will not be available to the parser. The IDE only scans the .ino file for libraries.

 

This entry was posted in Micro & Hardware, Programming and tagged , , , , , . Bookmark the permalink.

2 Responses to Multiple Source Files with the Arduino IDE

  1. max says:

    very helpful …..thank you very much =)

  2. electronicMe says:

    I tried to use the LiquidCrystal library in a cpp tab in the arduino sketch. I added the #include statement to the cpp file and it didn’t work (‘LiquidCrystal’ was not declared in this scope)
    After hours of searching for the reason why it didn’t worked i found this blog entry. I had to add the #include statement ALSO in the main ino file, as you said.

    Thank you sooo much for this post! You saved me a lot of time :DD

Leave a Reply