Go Down

Topic: #include libraries in files other than main (Read 445 times) previous topic - next topic

SukkoPera

I wrote a (badly) abstracted class that allows me to support both the ENC28J60 and the WIZ5100 Ethernet chips, through EtherCard and the official Ethernet library.

Basically I have a base class and then two different classes extending it that implement support for the two chips. Only one of them gets compiled at a time, depending on the value of a #define.

Everything is working great, but I have some problems with #includes. I would like to #include the relevant libraries only in the file defining the extended class, but if I do so, it seems the Arduino headers don't get included, as I get errors like:

Code: [Select]
In file included from HTTPRequestParser.h:25,
                 from WebServerBase.h:23,
                 from WebServer_ENC28J60.h:27,
                 from smartstrip.cpp:34:
Panic.h:60: error: 'byte' does not name a type
Panic.h:73: error: expected `)' before '_led'
Panic.h: In member function 'void Panic::infiniteBlink()':
Panic.h:65: error: 'led' was not declared in this scope
Panic.h:65: error: 'HIGH' was not declared in this scope
Panic.h:65: error: 'digitalWrite' was not declared in this scope
Panic.h:66: error: 'delay' was not declared in this scope
Panic.h:67: error: 'LOW' was not declared in this scope
Panic.h: In member function 'void Panic::setup()':
Panic.h:78: error: 'led' was not declared in this scope
Panic.h:78: error: 'OUTPUT' was not declared in this scope
Panic.h:78: error: 'pinMode' was not declared in this scope
Panic.h: In member function 'void Panic::panic(__FlashStringHelper*)':
Panic.h:94: error: 'Serial' was not declared in this scope
Panic.h: In member function 'void Panic::__assert_fail(const char*, const char*, int, const char*)':
Panic.h:106: error: 'Serial' was not declared in this scope


So, what I have to do is to put at the beginning of the sketch's main file:

Code: [Select]
#ifdef USE_ENC28J60
#include <EtherCard.h>
#else
#include <SPI.h>
#include <Ethernet.h>
#endif

#include "WebServer_ENC28J60.h"
#include "WebServer_WIZ5100.h"


But this breaks encapsulation, so I'd like to remove it. So there must be something I can't understand about the Arduino proprocessing. Can you help me?

The code is browseable at https://github.com/SukkoPera/smartstrip.
Thanks!

Tom Carpenter

If it is a seperate class, you will need to add the line:

#include "Arduino.h"

In order to get the type definitions. This is not needed in the sketch because the sketch is I believe an extension of main.cpp which already includes Arduino.h
~Tom~

SukkoPera

It's a separate class more in theory than in practice, since everything that is not in the sketch file is in a header file which is #included by the main sketch. Is there anything wrong with this approach?

Go Up