Hi guyz,
I would like to include many headers in only one to simply include the final one in my sketches.
Say I’ve many classes A, B, C,…that are related in some way (one can inherit from another or more and so on) and I would like to include all of them in my sketch without insert an #include statements for each one.
How can i do this?
Is it possible?
Just to try I’ve written only two classes, a.cpp and b.cpp with their respective headers (suppose I’ve many others classes).
a.h
#ifndef A_H
#define A_H
class A {
protected:
bool imA;
public:
A();
int doA();
};
#endif /* A_H */
a.cpp
#include "a.h"
A::A() {
imA = true;
}
int A::doA() {
return 1;
}
b.h
#ifndef B_H
#define B_H
#include "a.h"
class B : public A {
private:
bool imB;
public:
B();
int doB();
};
#endif /* B_H */
b.cpp
#include "b.h"
B::B() : A() {
imA = false;
imB = true;
}
int B::doB() {
return 2;
}
Then, I’ve written the final header MyLib.h which includes the others
MyLib.h
#ifndef MYLIB_H
#define MYLIB_H
#include "src/a.h"
#include "src/b.h"
#endif /* MYLIB_H */
As you can see from the last header, I’ve placed both classes in the src subdirectory of my library under the standard directory of Arduino libraries.
So, my directories tree is:
Arduino
`-libraries
`-MyLib
|-src
| |-a.cpp
| |-a.h
| |-b.cpp
| `-b.h
`-MyLib.h
Then I’ve written a trivial demo.cpp program to test if everything works.
demo.cpp
#include "MyLib.h"
#include <stdio.h>
int main() {
A *b = new B();
printf("%i\n", b->doA());
}
Once placed the previous program into the directory MyLib, It can be compiled (and then executed) without problems with this commad:
ervito@tecra:~/Arduino/libraries/MyLib$ g++ demo.cpp $(for header in $(ls src | grep "\.h$"); do printf "src/${header/%.h/.cpp} "; done;) -Wall -o demo
At the end I’ve tried to write a similar demo sketch for Arduino (test.ino)
test.ino
#include "MyLib.h"
void setup() {
A *b = new B();
b->doA();
}
void loop() {
// put your main code here, to run repeatedly:
}
TO BE CONTINUED…