Header files seem to be needed in main .INO file

I’m running into an issue with headers files. It seems that a header file needs to be included in the central .INO project file, even if the header isn’t needed in it but instead is used in a separate .CPP file.

Example 1. This compiles fine:

Example1.ino:
#include <Time.h>

void setup()
{
Serial.begin(9600);
}

void loop()
{
int h = hour();
}

Example2:
Example2.ino:
//#include <Time.h>
#include “MyClass.h”

void setup()
{
Serial.begin(9600);
}

void loop()
{
MyClass myClass;
myClass.PerformTest();
}

MyClass.h:
#ifndef MyClass_h
#define MyClass_h

class MyClass
{
public:
void PerformTest();
};

#endif // MyClass_h

MyClass.cpp:
#include <Time.h>
#include “MyClass.h”

void MyClass::PerformTest()
{
int h = hour();
}

When I compile example 2, event though I am including Time.h, I get the error:

MyClass.cpp: In member function ‘void MyClass::PerformTest()’:
MyClass.cpp:7: error: ‘hour’ was not declared in this scope

If I go back to Example2.ino, and un-comment #include <Time.h>
it compiles fine. This is not a convention in standard C/C++… since Example2.ino has no need (itself) for Time.h, it shouldn’t need to be included, and the dependency/scope should be limited to the files which need it (in this case, just MyClass.cpp), so I have a few questions:

  • Is this a known issue?
  • Is there a work around? (other than including every header within the project in the .INO file)

Thanks,

Jeff

PS. This is the 1.0.5-r2 version of the IDE, running on Windows 7 Pro x64 (if it matters)

Hi Jeff

It is a characteristic of the Arduino IDE.

More info here ...

http://arduino.land/FAQ/content/1/3/en/what-does-the-ide-change-in-my-sketch.html

Regards

Ray

It seems that a header file needs to be included in the central .INO project file, even if the header isn't needed in it but instead is used in a separate .CPP file.

Quite true.

  • Is this a known issue?

It's a known fact. Whether that makes it an issue is for you to decide.

  • Is there a work around? (other than including every header within the project in the .INO file)

You just described the correct process. Whether you consider that a workaround, or not, doesn't really matter. The Arduino team decided (rightly) that hiding the use of a library was not going to be allowed.

The arduino IDE only loads libraries that are included . If you want to use a library, include it. If you don't need it, comment it out. That is SOP.

@Ray

Thanks for the tip, I'll check it out.

@raschemmel

I don't think you understand the issue, though I thought I made it pretty obvious and simple with the sample I provided.

If you want to use a library, include it.

I am

If you don't need it, comment it out.

Yeppers, but that's not the issue. In the second example I provided, nothing within Time.h is needed in the code within the INO file. In a separate file, where declarations within Time.h are needed, I am including the header file. The issue is that, even though I am including the header in MyClass.cpp, I'm getting compile errors, and as I have discovered, one needs to add the header to the source in the INO file as well, even though none of the source within the INO needs it.

From what others have replied, it sounds like this is a known issue, so at least it's understood, though it's unfortunate as this is a practice discouraged when writing professional C/C++ (see "Effective C++" and "More Effective C++" by Scott Myers).

Jeff