PDA

View Full Version : What the ^&%$*$ is Arduino IDE doing with my header files?



Dozer
11-08-2012, 04:06 PM
Why can't I do this:

SimObjectsDev.ino:

#include "SimServoDev.h"

SimServo myServo (blah blah blah);

setup() ...
loop()...

SimServoDev.h:

#include <Servo.h> // behold the vanishing #include statement!

class SimServo {
...
private:
Servo _servo;
};

I'm writing a wrapper for the Servo library to make it very easy to connect servo-driven gauges to X-Plane. You can see the code here (https://github.com/JDeeth/SimObjectsDev).

My SimServo class is defined in the SimServoDev.h header file. SimServo contains a plain Servo as a member. I want to #include <Servo.h> within SimServoDev.h, but Arduino IDE is having none of it. When I do this, it complains that Servo does not define a type. Replace the angle-brackets with quote marks, it doesn't find the file.

The workaround is to #include <Servo.h> within the main sketch, just before #include "SimServoDev.h". Then everything works perfectly.

I understand the Arduino IDE is notorious for doing a preposterous amount of preprocessing, which I think is the culprit. (I discovered the typedef-as-function-argument trap earlier today too.) Can it be circumvented re header files? I can't abandon the Arduino IDE without finding another method of compiling code as easy-to-use for non-programmers. The purpose of the SimObject classes is to open up Teensy-based hardware to X-Plane users who aren't willing to learn much C++ (which is most of them).

The entire project is on GitHub (https://github.com/JDeeth/SimObjectsDev), which thinks it's in JavaScript because I uploaded Doxygen-generated autodocs alongside the source code.

Bill Greiman
11-08-2012, 05:51 PM
The IDE generates the search path for include files by scanning your sketch. When you add an include for Servo.h in your sketch, the IDE will add the library directory for Servo to the include path.

You can even do something like this in your sketch to get the IDE to find Servo.h


#if 0
#include <Servo.h>
#endif


I don't know a way around this.

Paul
11-08-2012, 10:52 PM
This is Arduino issue #236

http://code.google.com/p/arduino/issues/detail?id=236

Several months ago I almost fixed it. But as you can see in the comments, David changed his mind and decided this bug might be an Arduino feature. On stuff like this, I usually prefer to submit patches to Arduino rather than modify how it works only for Teensy. By the time he decided in July to accept a fix, I was already spending all my time on Teensy 3.0.

Dozer
11-09-2012, 01:12 AM
Ah, thanks for the explanation Paul! Interesting to see how this was seen as a 'feature' to make dependencies explicitly visible.

Instead of #include <foo.h> in my headers, I'll leave an allcaps comment // YOU MUST #INCLUDE <FOO.H> IN YOUR MAIN SKETCH!! instead.