Skip to main content
1 of 2
Cybergibbons
  • 5.4k
  • 7
  • 33
  • 51

Why do I need to include libraries used in other libraries?

I have a simple library which uses another library.

Here is the header:

#ifndef __DERIVEDCLASS_H__
#define __DERIVEDCLASS_H__

#include <HardwareSerial.h>

class DerivedClass
{
private:
    HardwareSerial* serial;

public:
    DerivedClass();
};
#endif

And the CPP:

#include "DerivedClass.h"

DerivedClass::DerivedClass()
{

}

And the sketch:

#include <DerivedClass.h>

void setup()
{
}

void loop()
{
}

This works fine.

However, if I change the library header to this:

#ifndef __DERIVEDCLASS_H__
#define __DERIVEDCLASS_H__

#include <SoftwareSerial.h>

class DerivedClass
{
private:
    SoftwareSerial* serial;

public:
    DerivedClass();
};
#endif

The sketch fails to compile:

/Users/andrew/Documents/Arduino/libraries/DerivedClass/DerivedClass.h:9: error: ISO C++ forbids declaration of 'SoftwareSerial' with no type /Users/andrew/Documents/Arduino/libraries/DerivedClass/DerivedClass.h:9: error: expected ';' before '*' token

Why is this? HardwareSerial is a built-in part of the Arduino, located in hardware/arduino/cores/arduino and SoftwareSerial is in libraries, but why would the build process differentiate between the two.

Cybergibbons
  • 5.4k
  • 7
  • 33
  • 51