3 Replies Latest reply on Jan 7, 2014 3:44 AM by James.Giller

    How to include pre-defined library .h files in own .h files?

    James.Giller

      Hi,

       

      I want to extend the USBHost library to allow my Galileo to connect to a Lego Mindstorms NXT. I have written two files: nxt.h & nxt.cpp, attached.

      Obviously I have to use certain things that are defined in various .h files in the USBHost library, so I must include those files in my nxt.h file.

      Taking Usb.h as an example, I have tried adding both

      # include "Usb.h"

      and

      # include <Usb.h>

      to the top of nxt.h, but I always get the error "nxt.h:2:17: fatal error: Usb.h: No such file or directory".

       

      How can I include Usb.h and other .h files from the USBHost library?

       

      Thank you very much.

       

      James

       

      Message was edited by: James Giller  By the way, nxt.h and nxt.cpp are saved in a subdirectory of the libraries folder in my sketchbook, where one is supposed to place user defined libraries.

        • 1. Re: How to include pre-defined library .h files in own .h files?
          Clayton Hofrock

          The way that most Arduino libraries are written and used is that they are placed inside a folder (you could call it NXT). That folder contains the .cpp file and the .h file. It can also include a keywords.txt (a file for new datatypes, methods/functions, and new constants that your library has). It can also include an example folder with sketches that use your library.

           

          This folder is placed in the arduino/libraries/ folder. Once you put the folder there you need to restart your Arduino IDE. After restarting the IDE you should be able to go to Sketch -> Import Library...  and your new library NXT should be in the list of libraries. Seeing it in the list lets you know that the IDE can find your files. (you can click on the library name, but all it does is add the include line at the top of your sketch)

          • 2. Re: How to include pre-defined library .h files in own .h files?
            James.Giller

            Hi chofrock,

             

            Thank you for your response; however, you misunderstand my question.

            There are 3 steps I must take to get my application to work:

             

            1) nxt.h - # include various .h files from arduino-1.5.3\hardware\arduino\x86\libraries\USBHost (e.g. Usb.h)

            2) nxt.cpp - # include nxt.h (in order to implement functions etc.)

            3) sketch - # include nxt.h (in order to use my NXT library)

             

            Your response relates to step 3, but I do not have a problem with linking user created libraries with sketches.

             

            My problem is with step 1. I keep getting a 'no such file or directory' error when I attempt to include .h files from the USBHost library into nxt.h.

            I presumed that at the top of my nxt.h file I could just write either

            # include <Usb.h> or

            # include "Usb.h"

            and everything would be fine, but I keep getting a 'no such file or directory' error.

             

            I guess this is because nxt.h is part of a library and is not a sketch file, and I know that .h and .cpp files from libraries are compiled seperately from the main sketch. Perhaps when .h and .cpp files are compiled, the compiler looks in a different directory for includes.

            I just want to know how to include .h files from libraries in arduino-1.5.3\hardware\arduino\x86\libraries\USBHost into my nxt.h file?

             

            Thanks again,

             

            James.

            • 3. Re: How to include pre-defined library .h files in own .h files?
              James.Giller

              I found a stackoverflow thread about this issue that contains a workaround.

               

              c++ - Is it possible to include a library from another library using the Arduino IDE? - Stack Overflow

               

              Apparently this is an historic Arduino issue that has not yet been addressed.