Creating a library, can i import another library?

Hi guys!

i’m trying to write a library. It depends from other libraries (Ethernet and others). Is this posible? I refer to depend another library?

Here it’s the code:

myClass.h :

#ifndef myClass_h
#define myClass_h
#include <Arduino.h>
#include <Ethernet.h>
 
class myClass
{
public:
    void mySetup(void);
};
#endif

myClass.cpp:

#include "myClass.h"
 
void myClass::mySetup(){
    Serial.begin(9600);
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
    //the IP address for the shield:
    byte ip[] = { 192, 168, 0, 200 };
    Ethernet.begin(mac, ip);
    //while ( != 1)
    while(true)
    {
        Serial.println("Error getting IP address via DHCP, trying again...");
        delay(15000);
    }
}

my sketch:

#include <myClass.h>

void setup(){
  
}

void loop(){
}

I got this error:

/home/roberto/herramientas/arduino/arduino-1.0.5/libraries/myClass/myClass.cpp: In member function ‘void myClass::mySetup()’:
/home/roberto/herramientas/arduino/arduino-1.0.5/libraries/myClass/myClass.cpp:8: error: ‘Ethernet’ was not declared in this scope

What am i doing bad?

Thanks for any clue!

Greets,

Roberto

In myClass.cpp try forcing the include for Ethernet.h

#include "myClass.h"
#include <Ethernet.h>
 
void myClass::mySetup(){
...

You can import any library you like into your library. However, the IDE won't know you've imported that library when you come to compile, so it won't add that library's folder to the path of files to include and compile.

You have to tell the IDE that your library includes the other library by also including that library in your main sketch.

It's messy, I know, but it's the way it is I'm afraid.

This wouldn't be a problem with UECIDE though, which has recursive library discovery.

nid69ita:
In myClass.cpp try forcing the include for Ethernet.h

#include "myClass.h"

#include <Ethernet.h>

void myClass::mySetup(){

this was the first thing that i try :frowning:

i forgot to add it.

Wouldn't it be necessary to instantiate an "Ethernet" object within myClass or pass it into myClass?

Ethernet.cpp does try to create one for you:

EthernetClass Ethernet;

at the very end of it. Evidently that is out of scope within myClass.

@Majenko is right.
How Arduino IDE collect includes from directory is not simple

But if you include in your sketch ALSO spi.h and ethernet.h it compile !!!

#include <SPI.h>
#include <Ethernet.h>
#include <myClass.h>

myClass pippo;

void setup(){}

void loop(){}

For the same problem Ethernet library ask that you call also SPI.h before Ethernet.h include.

nid69ita, it works perfect!

I have discovered, that the problem it was in the sketch but i don't understand what is happening there.

The Ethernet.h is declared in the .h and .cpp... why does the problem is reported in cpp file...

There is a particular order to do the imports?

Thank very much!

greets,
Roberto

You can read the post of @majenko
http://forum.arduino.cc/index.php?topic=183326.msg1358286#msg1358286

and also activate in preference the voice verbose to see what IDE do when compile.

Usally in another IDE like Visual Studio C++ you must add in Project .h and .c/.cpp necessary.
Arduino IDE make a temporary directory with all .h and .c/.cpp necessary I think analizing the sketch and not libraries.