Librarys' within Librarys

Hi,

I’m having trouble referencing a library with a library.

I started with two working and tested library, the trouble started when I attempted to reference one from the other. A quick search on this forum suggested that replacing this…
#include “Base.h”
With a hardcoded full path…
#include “C:\all_apps\arduino-0021\libraries\Base\Base.h”

Would get rid of the error…
C:\all_apps\arduino-0021\libraries\MessMan/MessMan.h:14:44: error: Base.h: No such file or directory

This worked (or appeared to).

However I now discover that as soon as I attempt to use any functions within that class I know get errors like this…
C:\all_apps\arduino-0021\libraries\MessMan/MessMan.cpp:113: undefined reference to BASE91::BASE91()' C:\all_apps\arduino-0021\libraries\MessMan/MessMan.cpp:142: undefined reference to BASE91::ToBase91(char*)’
C:\all_apps\arduino-0021\libraries\MessMan/MessMan.cpp:153: undefined reference to `BASE91::ToBase91(char*)’

I’ve gone back to the forum but can’t find any fixes for this, in fact I’m not sure it there are any fixes, am I attempting to do something that isn’t possible? Are there any fixes or work-arounds to this?

Regards

Mike

A quick search on this forum suggested that replacing this.....

include "Base.h"

With a hardcoded full path.....

include "C:\all_apps\arduino-0021\libraries\Base\Base.h"

I think that you misread something. The idea is to locate Base.h using a relative path, not an absolute path:

#include "..\Base\Base.h"

This worked (or appeared to).

Still, using relative paths is better, because both libraries can change parents, with a new version of Arduino, and not need to be edited. Your way will require editing a library.

However I now discover that as soon as I attempt to use any functions within that class I know get errors like this

The Arduino compiler knows which files to compile and link. The files to compile and link are the sketch(es) in the current directory, and any .c or .cpp files that correspond to header files included in those sketches.

There is no way to use a library in your library without the sketch knowing that.

Hi PaulS,

There is no way to use a library in your library without the sketch knowing that

I'm still confused, are you saying there is no way to include one library within another?

Is my only solution to move (i.e. cut n paste) the code I'm interested from the 'Base.h' library into the second library? I can do this since the code isn't actually that long, but it's not really a very elegant solution.

Regards

Mike

I’m still confused, are you saying there is no way to include one library within another?

No, that is not what I said. I said:

There is no way to use a library in your library without the sketch knowing that

Ok,

so what’s the fix?

I’ve changed my include statement to a relative path as suggested, but still get the same “undefined reference” errors described in my original post.

I assume that these errors indicate that the compiler can’t find the class (or the methods within the class).

Regards

Mike

Ok, so what's the fix?

The compiler looks at the sketch to determine what needs to be compiled. If the sketch says that it includes MyLibrary.h, then the sketch and MyLibrary.cpp will be compiled and linked.

If MyLibrary.h or MyLibrary.cpp tries to include another library, that library will not be compiled and linked, and you will get the undefined messages that you are getting.

The solution is to stop trying to hide from the sketch the fact that MyLibrary references Base.

Include Base.h in the sketch.

I think I've mentioned this twice before, now.

Include Base.h in the sketch

I finally get it! So what we're saying is that even though the top level sketch doesn't use Base.h directly itself, it still needs to be included so that the library that does use it will work.

Thanks for your patience!

Ok, I think I’ve finally got this fixed. This seems to be a recurring issue, so I’ll attempt to summarize both the problem and the solution here for anyone else who encounters this.

The problem???

I’ve written two libraries we’ll call them MyLibrary.h and MySubLibrary.h. Both have been tested on their own and both work as expected. The problem comes when I want to include MySubLibrary.h in MyLibrary.h.

My sketch has the line “#include < MyLibrary.h>” (since it needs to use that library), but doesn’t mention MySubLibrary.h because it doesn’t use it directly. I try to compile the library, and get errors like this…
MyLibrary.cpp:113: undefined reference to MySubLibrary:: MySubLibrary ()' MyLibrary.cpp:142: undefined reference to MySubLibrary::ToBase91(char*)’
Where ToBase91 is a function/method in MySubLibrary.

The solution(s)??
There are several solutions suggested on this forum. The first suggested that when I added MySubLibrary to MyLibrary, I used the line ’ #include MySubLibrary.h" '. This is wrong, it needs to be ’ #include “…\ MySubLibrary\MySubLibrary.h” ’

I tried this, and this got rid of the first of the two errors listed above.

The 2nd suggestion was that the sketch itself should include MySubLibrary.h despite not actually using it directly. I added a suitable include statement but this didn’t actually make any difference.

The final thing I tried was to copy the files MySubLibrary.cpp and MySubLibrary.h into the same directory as the MyLibrary files. This finally got rid of the last remaining error.

One last check I made was to undo the first two fixes, the solution still worked! So, in summary if you’re having trouble including one library in another, just copy the .h and .cpp into the same directory.

Regards

You should probably avoid making a copy of the sub-library. Instead, #include it in both the containing library, so it can use the things defined in the sub-library, and in the sketch, so that the sub-library is compiled and linked during the build process.