Accessing Arduino Libraries from Tabs

I assume there's something simple I'm overlooking here. I'm using the Arduino IDE 1.0.1 on OS X (10.7.5) with all updates applied.

I have a sketch (.ino) with a couple of tabs (.h and .cpp files). I'm trying to include the Servo class as a member field of a class I'm creating.

If I create the class declaration from within the .ino file, the code compiles file. If I move the class declaration to a .h file, the compiler complains.

An example from within the single .ino file. This compiles just fine.

#include <Servo.h>

class A
{
  Servo servo;
};

A a;

void setup() {}
void loop() {}

If I move the class declaration to a .h file, I get the error

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

Here is the .ino file:

#include "test.h"

A a;

void setup() {}
void loop() {}

And here is the .h file:

#ifndef TEST_H
#define TEST_H

#include <Servo.h> 

class A
{
  Servo servo;
};

#endif//TEST_H

The error suggests it hasn't reached the declaration for the Servo class, but I'm not sure how that's possible without reporting that it can't find Servo.h.

As a work-around, I can include <Servo.h> in the .ino file before including the .h file.

This doesn't explain the behaviour, but it does allow me to continue my work in the meantime.

It acts as if the include path for the .h file is different than the .ino file.

#include <Servo.h> // <-------
#include "test.h"

A a;

void setup() {}
void loop() {}

This doesn't explain the behaviour, but it does allow me to continue my work in the meantime.

You obviously haven't read any of the other threads that have asked the same question. The IDE is designed to NOT allow you to hide the use of a library from the sketch.

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.

PaulS:
You obviously haven't read any of the other threads that have asked the same question.

Obviously not. :slight_smile: But it wasn't for lack of trying to find an answer.

The IDE is designed to NOT allow you to hide the use of a library from the sketch.

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 is a terrific answer. Concise, and informative. I could quibble with the philosophy, but it is what it is and will run with it.

I appreciate your reply.