(Library inception) Including a library inside a library!

Hi everyone,

I was wondering if anyone could give me a helping hand. I'm working on an Arduino shield and I'm creating a library for it in order to interface with it.

I would like to use the SoftwareSerial as well as a 3rd party library inside my library. However, when I compile, I get the following errors:

warning: SoftwareSerial.h: No such file or directory
'SoftwareSerial' does not name a type

Seem like it's not linking up.

Now the 3rd party library is specifically Rogue Robotics' Tone library. That one I can include fine without any compile errors. But when I call Tone.begin(), I get the following:

undefined reference to `Tone::begin(unsigned char)

This is how I declare the libraries in my header file.

#ifndef Gamer_h
#define Gamer_h

#include "Arduino.h"
#include <avr/interrupt.h>
#include <avr/io.h>
#include "Tone/Tone.h"
#include <SoftwareSerial.h>

class Gamer {
public:
void begin();
SoftwareSerial serial;
Tone buzzer;
#define RX 5
#define TX 4
};
#endif

And this is how I try to instantiate them in my cpp file.

#include "Gamer.h"

void Gamer::begin() {
	buzzer.begin(2);
	serial = SoftwareSerial(RX, TX)
}

If you guys need any more information, please give me a shout. I would love a helping hand :slight_smile:

This is how I declare the libraries in my header file.
And this is how I try to instantiate them in my cpp file.

And your sketch? I'm guessing that you're trying to hide the use of SoftwareSerial and the other library from the sketch. The sooner you get over the idea that that is a good idea (or even possible), the happier you'll be.

Hi Paul,

Thanks so much for the swift reply.

My sketch is the following:

#include <Gamer.h>

Gamer gamer;

void setup() {
  gamer.begin();
}

void loop() {

}

I know I'm asking for trouble trying to do this, but from a user's perspective it would be great if the library included any other libraries that are needed. At this point, I just wanna know if it's even possible!

At this point, I just wanna know if it's even possible!

Yes, it is possible if the sketch includes all the same header files.

If not, then you need to use something other than the IDE to compile your code.

Add

#include <SoftwareSerial.h>

to your sketch. Then the IDE will be able to compile the code.

Hello.
I have the same problem.
For the accuracy of the experiment I copied the code above comrade mikevanis, exclude tone.h. My Arduino IDE 1.0.5 shows an error:

D:\Program Files (x86)\Arduino\libraries\Gamer/Gamer.h: In constructor 'Gamer::Gamer()':
D:\Program Files (x86)\Arduino\libraries\Gamer/Gamer.h:10: error: no matching function for call to 'SoftwareSerial::SoftwareSerial()'
D:\Program Files (x86)\Arduino\libraries\SoftwareSerial/SoftwareSerial.h:83: note: candidates are: SoftwareSerial::SoftwareSerial(uint8_t, uint8_t, bool)
D:\Program Files (x86)\Arduino\libraries\SoftwareSerial/SoftwareSerial.h:48: note:                 SoftwareSerial::SoftwareSerial(const SoftwareSerial&)
sketch_oct26a.ino: At global scope:
sketch_oct26a.ino:4: note: synthesized method 'Gamer::Gamer()' first required here

Gamer.h:10:

class Gamer {

My sketch:

#include <Gamer.h>
#include <SoftwareSerial.h>

Gamer gamer;

void setup() {
  gamer.begin();
}

void loop() {

}

Sorry for my english.

Help me)

I have the same problem.

Would that be the "I can't be bothered posting all my code" problem?

PaulS:

At this point, I just wanna know if it's even possible!

Yes, it is possible if the sketch includes all the same header files.

If not, then you need to use something other than the IDE to compile your code.

Add

#include <SoftwareSerial.h>

to your sketch. Then the IDE will be able to compile the code.

This is 100% correct.
The IDE builds his link build path based on the includes in the .ino file only.
So having the includes in your include file is not enough.
Best regards
Jantje

Ok.
File Gamer.h:

#ifndef Gamer_h
#define Gamer_h

#include "Arduino.h"
#include <avr/interrupt.h>
#include <avr/io.h>
//#include "Tone/Tone.h"
#include <SoftwareSerial.h>

class Gamer {
public:
void begin();
SoftwareSerial serial;
//Tone buzzer;
#define RX 5
#define TX 4
};
#endif

File Gamer.cpp:

#include "Gamer.h"
#include <SoftwareSerial.h>

void Gamer::begin() {
//	buzzer.begin(2);
	serial = SoftwareSerial(RX, TX)
}

Sketch in my first post.

It does not compile by Arduino IDE 1.0.5. What is my mistake?

Jantje:
The IDE builds his link build path based on the includes in the .ino file only.

Dimsan:
File Gamer**.cpp**:

It does not compile by Arduino IDE 1.0.5. What is my mistake?

SoftwareSerial serial;

This is trying to create an instance of the SoftwareSerial class, but, obviously it can't because the SoftwareSerial class does not, unfortunately, have a no argument constructor.

One solution:
SoftwareSerial *serial;

serial = new SoftwareSerial(RX, TX);

Then, use serial->, instead of serial. to access member of the SoftwareSerial class.

Dimsan:
My sketch:

#include <Gamer.h>

#include <SoftwareSerial.h>

Gamer gamer;

void setup() {
 gamer.begin();
}

void loop() {

}

Compiler must know it BEFORE you include it inside your class...
Correct order:

#include <SoftwareSerial.h>
#include <Gamer.h>

Compiler must know it BEFORE you include it inside your class...
Correct order:

The order does not matter. The fact that both are included matters. Well, that and the fact source and header files are structured correctly.

PaulS:

Compiler must know it BEFORE you include it inside your class...
Correct order:

The order does not matter. The fact that both are included matters. Well, that and the fact source and header files are structured correctly.

Try it as I said... I'm shure of it :wink:

Note: You also have to include the library inside your library... So, there will be two includes of the same file.

PaulS:
One solution:
SoftwareSerial *serial;

serial = new SoftwareSerial(RX, TX);

Thanks, PaulS, it works.

Just for future reference:
http://provideyourown.com/2011/advanced-arduino-including-multiple-libraries/