Go Down

Topic: #include a standard library in a custom library (Read 2458 times) previous topic - next topic

fj604

Hi All,

Is there a way to #include a standard library from a custom library without copying the standard sources to the custom library folder?

PaulS

The #include statement can include relative paths, so, yes, you can do this.

Bear in mind that the compiler searches the sketch to see what to compile, so if only your library is included in the sketch, only your library will be compiled, and the other library will not.

fj604

Thanks for your reply PaulS,

What if I just want to #include LiquidCrystal.h without knowing where it lives?

If i do this:
Code: [Select]
#include <LiquidCrystal.h>

, then the compiler complains that it cannot find it, so I have to copy LiquidCrystal.h and LiquidCrystal.cpp to my custom library folder and use:
Code: [Select]
#include "LiquidCrystal.h"

Is there a nicer way?

PaulS

Quote
What if I just want to #include LiquidCrystal.h without knowing where it lives?

You can't.

Quote
so I have to copy LiquidCrystal.h and LiquidCrystal.cpp to my custom library folder and use:

No. This implies that you do know where LiquidCrystal.h lives.

You library and LiquidCrystal should have the same parent. So, you should be able to
Code: [Select]
#include <..\LiquidCrystal\LiquidCrystal.h>
in your library.

In your sketch, you need
Code: [Select]
#include <YourLibrary.h>
#include <LiquidCrystal.h>

fj604

PaulS, thanks again for your reply - I understand there is no nice way - as my library lives in <sketch folder>\libraries\MyLibrary and LiquidCrystal.h is in <Arduino folder>\libraries\LiquidCrystal.

So I have to either include LiquidCrystal.h in the sketch, or include it in MyLibrary.h and copy LiquidCrystal sources to <sketch folder>\libraries\MyLibrary.


PaulS

The need to include LiquidCrystal.h in the sketch is not dependent on where the LiquidCrystal library is relative to your library. It MUST be included in the sketch.

You can not hide from the sketch the fact that you are using LiquidCrystal.

fj604

#6
Dec 11, 2010, 06:32 pm Last Edit: Dec 11, 2010, 06:33 pm by fj604 Reason: 1
PaulS,

Apologies, perhaps I have not explained it correctly.

The library has a class that inherits LiquidCrystal, so I thought it would be better to include LiquidCrystal header into MyLibrary.h

The way I found to hide LiquidCrystal.h from the sketch is to:
1. Copy LiquidCrystal.h and .cpp to MyLibrary folder
2. in MyLibrary.h add:
Code: [Select]
#include "LiquidCrystal.h"

Is this not considered good practice?

PaulS

Quote
Is this not considered good practice?

No it isn't. The reason is that any computer that gets your library installed on it now has two copies of LiquidCrytal.h and LiquidCrystal.cpp.

While this may not seem like a big deal, what happens if the LiquidCrystal library gets updated? Now, the two copies are not the same. A sketch that uses LiquidCrystal (why couldn't that have been called LQ? Much easier to type) and needs the new behavior will work, until the owner tries using your library.

Consider, too, what happens if someone wants to derive from your library. Now, there need to be 3 copies of LiquidCrystal on the computer.

Soon, it becomes a nightmare to try to keep all the copies of LiquidCrystal up-to-date.

In my opinion, it is better to be up front about that fact that your library depends on LiquidCrystal.

fj604

Hi PaulS,

I see your point and "I cannot fail to disagree with you less" ;)

(4 negations = total agreement)

I will make the changes you suggested.

Thank you for your valuable input.

fj604

#9
Dec 12, 2010, 01:41 am Last Edit: Dec 12, 2010, 01:47 am by fj604 Reason: 1
It could have been better though - I still don't like the fact that I have to include LiquidCrystal.h using the absolute or relative path.

The LiquidCrystal library inherits the Print class from core library:
Code: [Select]
#include "Print.h"

My understanding is that my library cannot do:
Code: [Select]
#include "LiquidCrystal.h"
only because LiquidCrystal is not in the core libraries and therefore cannot be found at compile time. It would have been nice if the path to non-core libraries such as LiquidCrystal was included in the default library search path at compile time.

Perhaps this can be corrected in future versions of Arduino software? Or is there a better way?

mellis

You can include the depended-on library from the sketch as well as from your library, then it will be included in the search path.

Go Up