Referencing one library within another.

Hi,

I'm using this led bargraph....

I've downloaded and installed the two adafruit libraries that come with it, and I've run the example sketches without problems.

I want to build my own library to use this bargraph, but I'm getting problems before I even begin to include any of my own functionality. Here's my library....
AsiBargraph.h

#ifndef AsiBargraph_h                        
#define AsiBargraph_h                       

extern "C" {
  #include <string.h>
}

#include <stdlib.h>                        
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"                       
#else
#include "WProgram.h"                      
#endif
#include <Wire.h>
#include <Adafruit_LEDBackpack.h>
#include <Adafruit_GFX.h>
#include <AsiBargraph.h>                   // header for this library


class AsiBargraph
{
  public:
    AsiBargraph();                            // Constructor
    void begin(void);                         // Setup object

  private:
    // Member variables

    Adafruit_24bargraph _asiBar;

    // Private functions                    
};

#endif

AsiBargraph.cpp

extern "C" {
  #include <string.h>
}

#include <stdlib.h>                       
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"                       
#else
#include "WProgram.h"                    
#endif

#include <Wire.h>
#include <Adafruit_LEDBackpack.h>
#include <Adafruit_GFX.h>
#include <AsiBargraph.h>                   // header for this library


/**************************************************************************
  Constructor
**************************************************************************/
AsiBargraph::AsiBargraph()
{
}

/**************************************************************************
  Do stuff
**************************************************************************/
void AsiBargraph::begin(void)                       
{
}

As you can see, I've stripped all actual functionality from the library, but as soon as I compile it I get the following errors...

In file included from C:\all_apps\arduino-1.0\libraries\AsiBargraph/AsiBargraph.h:39,
                 from AsiBargraphTest.cpp:29:
C:\all_apps\arduino-1.0\libraries\Adafruit_LEDBackpack/Adafruit_LEDBackpack.h:56: error: redefinition of 'class Adafruit_LEDBackpack'
C:\all_apps\arduino-1.0\libraries\Adafruit_LEDBackpack/Adafruit_LEDBackpack.h:56: error: previous definition of 'class Adafruit_LEDBackpack'
C:\all_apps\arduino-1.0\libraries\Adafruit_LEDBackpack/Adafruit_LEDBackpack.h:72: error: redefinition of 'class Adafruit_AlphaNum4'
C:\all_apps\arduino-1.0\libraries\Adafruit_LEDBackpack/Adafruit_LEDBackpack.h:72: error: previous definition of 'class Adafruit_AlphaNum4'

I'm sure this is one of those 'staring you in the face errors', the adafruit libraries have been installed and work, the hardware works, the examples compile and run without problem, but as soon as I include the libraries in another one I start getting errors. What have I overlooked?

Thanks

#include <AsiBargraph.h>                   // header for this library

One does NOT include the header file in the header file. Recursion is not a good thing.
One does NOT include the header file in the header file. Recursion is not a good thing.
One does NOT include the header file in the header file. Recursion is not a good thing.
One does NOT include the header file in the header file. Recursion is not a good thing.
One does NOT include the header file in the header file. Recursion is not a good thing.
One does NOT include the header file in the header file. Recursion is not a good thing.
One does NOT include the header file in the header file. Recursion is not a good thing.

To be honest, I've done this since I started working with arduinos, because it's how the tutorial says it should be done!

Must admit, I always thought it seemed a bit strange!

Anyway, I've removed the offending line, but am left with exactly the same error!

because it's how the tutorial says it should be done!

You want to quote the part that says that?

Anyway, I've removed the offending line, but am left with exactly the same error!

You need to look at the Adafruit_LEDBackpack library, to see that it is properly written.

And, of course it isn't. The definitions of the classes belong in the header file. The implementation goes in the source file. For some unfathomable reason, Adafruit tends to write all their libraries with the implementation in the header file.

If you want to build a library that includes those libraries, you'll need to re-architect them to be done properly.

You want to quote the part that says that?

Nope.
You're right, I made a mistake, it says to add the reference to the .cpp file, not the .h file.

And, of course it isn't. The definitions of the classes belong in the header file. The implementation goes in the source file. For some unfathomable reason, Adafruit tends to write all their libraries with the implementation in the header file.

Ahh! I made the mistake of assuming they knew what they were doing, so I though the error must be in my code.

1st rule of IT - never assume!

Thanks

For some unfathomable reason, Adafruit tends to write all their libraries with the implementation in the header file.

I'm not sure that's the case here. Every statement in Adafruit_LEDBackpack.h seems to be a #define or a class definition.

I've done a bit more digging (read another tutorial) and have fixed the problem by adding these two lines to the top of the header file....

#ifndef Adafruit_LEDBackpack_h                       
#define Adafruit_LEDBackpack_h

and this line at the bottom

#endif

That seems to have fixed it, my code now compiles and runs.

thanks for the help.

cheers

Every statement in Adafruit_LEDBackpack.h seems to be a #define or a class definition.

Well, hell, I don't know what I was looking at. I swear they changed the code on GITHUB between the two times I looked at it. Yep, that's my story, and I'm sticking to it.