Go Down

Topic: Error while referencing a library class from .h file (Read 539 times) previous topic - next topic

vadim_us

Feb 15, 2013, 07:28 pm Last Edit: Feb 15, 2013, 07:48 pm by vadim_us Reason: 1
Hi,
I'm having problem trying to refer to IPAddress class from my own .h file. This compiles:

Main.ino:
Code: [Select]

#include <SPI.h>
#include <Ethernet.h>

class TT
{
 public:
   IPAddress ia;
};

void setup()
{

 /* add setup code here */

}

void loop()
{

 /* add main program code here */

}


and this does not:

Main.ino:
Code: [Select]

#include "Test.h"

void setup()
{

 /* add setup code here */

}

void loop()
{

 /* add main program code here */

}


Test.h (in a same folder):
Code: [Select]

// Test.h

#ifndef _TEST_h
#define _TEST_h

#include <SPI.h>
#include <Ethernet.h>

class TT
{
 public:
   IPAddress ia;
};

#endif


The error is:
In file included from Main.ino:2:
/Test.h:6:17: warning: SPI.h: No such file or directory
/Test.h:7:22: warning: Ethernet.h: No such file or directory
In file included from Main.ino:2:
Test.h:12: error: 'IPAddress' does not name a type

I don't have a lot of experience with C++ so this maybe something obvious.

PaulS

You appear to be doing what a lot of people would like to do, but no one can. That is, you are trying to hide the use of a library from the sketch. You can't do that. Every library that the sketch uses, directly or indirectly must be included in the sketch.

vadim_us

Thank you, I've just figured that out. However, when I try to simulate my project with Microsoft C++ compiler (these files were just examples), it compiles flawlessly. So, it seems that this behavior is a quirk of g++ compiler Arduino uses (or, a benefit of Microsoft compiler, if you wish).

PaulS

Quote
So, it seems that this behavior is a quirk of g++ compiler Arduino uses (or, a benefit of Microsoft compiler, if you wish).

It's neither one. The IDE copies files from the sketch directory and libraries directory to a temparary build directory. The compiler and linker are run against files in that directory.

So, what does it copy? Any file in the sketch directory and any file included in the sketch. What is not copied? Files included by files included by the sketch.

So, when the sketch only includes Test.h, Ethernet.h (and .cpp) and SPI.h (and SPI.cpp) are not copied. So, those header files are not found.

Go Up