Accessing library code from a .cpp/.h file?

I can access library code from my main sketch no problem. But, when I have a .h file in the same directory as my sketch I can't seem to be able to include any library .h files in it. #include <xxx.h> doesn't work nor does #include "xxx.h" I'm sure this has been run across before. I even went as far as tried adding a library from the IDE menu in the .h file I was wrking on and that didn't work either. Anyone have the usual work around? Fix?

Help?

Thanks!

-jim lee

jimLee:
I can access library code from my main sketch no problem. But, when I have a .h file in the same directory as my sketch I can't seem to be able to include any library .h files in it. #include <xxx.h> doesn't work nor does #include "xxx.h" I'm sure this has been run across before. I even went as far as tried adding a library from the IDE menu in the .h file I was wrking on and that didn't work either. Anyone have the usual work around? Fix?

Help?

Thanks!

-jim lee

Are you talking about a .h file that you dropped into your sketches folder? Did you close and reopen the IDE? Can you create a new file within the IDE?

jimLee:
But, when I have a .h file in the same directory as my sketch I can't seem to be able to include any library .h files in it.

You need to make a "new tab".

Although simply closing the sketch and re-opening works I think, because the IDE scans the folder and finds the file then.

The reason is that when it compiles it copies your files to a temporary area for compilation. If you just drop the file into the folder it doesn't realize it is there.

I've tried dropping in new files and reopening.. No luck I've tried adding new tabs, then "importing library" into the .h file to let the IDE write the #include.. No luck.

I can get the silly thing to compile if I do the #include of the library file .h into both the sketch file and my .h file. But that's such a hack..

-jim lee

When I mentioned the IDE copying your files to a temporary area, it copies your sketch, your other tabs, and files included in the main sketch. I don't think it does a "nested" look for .h files.

So, for example, if a library uses Wire.h, you have to include Wire.h in the main sketch to trigger this behaviour.

I don't think you need to put an include for Wire.h (say) in the .h file of your other tab, because that will be already included (if you get the order right). But if you have another .cpp file then you also need the include in that, which is reasonable, I think.

I kinda' see what your saying. I think I've run into some clunky glue code.

So.. is this correct?

my.h file needs xxx.h to compile BUT it won't be able to see xxx.h unless xxx.h is also included in my sketch file?. Hence, I need to include them in both.

Right?

-jim lee

Yes, but to put it into context you don't "compile" .h files.

So the .cpp file that includes the .h file needs to include any dependent libraries as well (first).

Wait a sec.. The .cpp needs to include the dependent libraries? Crazy souce, I'll try it.

-jim lee

The compiler compiles your sketch (.pde/.ino) plus any extra .cpp or .c files you have in your "other tabs". Those are what you should concentrate on. The .h files are just a convenience to include commonly-used stuff in multiple places.

Well, the idea was to simplify things. Isn't it always?

I bought this nifty color touch screen thing. I didn't like all the #includes and setup nonsense in my sketch so I was wrapping up everything it its own screenObj class. The plan was to let that handle all the messy bits. Hence the .h & .cpp files. Its hard to "hide complexity" when the #includes keep leaking out.

-jim lee

Well like I said, I have to include Wire.h in the main sketch if I want to use the Wire library, even if that library is totally used from other libraries.

There is a school of thought that this is good documentation. That is, by looking at the main sketch alone you can see which libraries it depends on.