Pages: [1]   Go Down
Author Topic: Error while referencing a library class from .h file  (Read 380 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I'm having problem trying to refer to IPAddress class from my own .h file. This compiles:

Main.ino:
Code:
#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:
#include "Test.h"

void setup()
{

  /* add setup code here */

}

void loop()
{

  /* add main program code here */

}

Test.h (in a same folder):
Code:
// 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.
« Last Edit: February 15, 2013, 01:48:23 pm by vadim_us » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46224
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46224
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: