Including Libraries in the Arduino IDE

There’s an annoying subtlety in the Arduino IDE when using tabs (multiple files in the sketch), that was kindly explained by PaulS on the Arduino forums.

Any Arduino libraries that are needed by the files outside of the sketch (.ino) file must also be listed in the sketch file itself.

Example

You cannot simply do this:

example.h:

#include <Servo.h>

When you attempt to compile, you’ll get an error like,

In file included from test_junk_02.cpp:1:
test.h:7: error: 'Servo' does not name a type

You must also include the library in the sketch:

test.ino:

#include <Servo.h>
#incude "example.h"

Explanation

The explanation that PaulS gave was the following:

The sketch is parsed for include files. The sketch, all included header files, and the corresponding source files, are copied to another directory for compiling. From that directory, library-based include files are NOT available unless they are included in the sketch and copied to the build directory.

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

2 Responses to Including Libraries in the Arduino IDE

  1. Derek Adams says:

    Thanks for the post! You just fixed a problem that caused me hours of headaches.

  2. Mishel Johns says:

    Same here. Thank you!

Leave a Reply