Go Down

Topic: Using system library (EEPROM) in my custom one (Read 1 time) previous topic - next topic

lucadentella

Hello!

I'm working on a custom library and I need EEPROM functions in it... so I thought I could include it in my header file:

Code: [Select]
#ifndef MyTestLibrary_h
#define MyTestLibrary_h

#include "WProgram.h"
#include "..\EEPROM\EEPROM.h"

#define ADDRESS_LOCATION 511
#define DEFAULT_ADDRESS 0x03

class MyTestLibrary
{
public:
MyTestLibrary();
void begin();

private:
byte node_address;
void getNodeAddress();
void storeNodeAddress();
};

#endif


Code: [Select]
#include "MyTestLibrary.h"

MyTestLibrary::MyTestLibrary() {

}

void MyTestLibrary::begin() {

getNodeAddress();
}

void MyTestLibrary::getNodeAddress() {
 
node_address = EEPROM.read(ADDRESS_LOCATION);
if(node_address == 0xFF) {
node_address = DEFAULT_ADDRESS;
storeNodeAddress();
}
}

void MyTestLibrary::storeNodeAddress() {
 
EEPROM.write(ADDRESS_LOCATION, node_address);
}


But when I try to use it in a simple sketch:

Code: [Select]
#include <MyTestLibrary.h>

MyTestLibrary myTestLibrary;

void setup() {
 
  myTestLibrary.begin();
}

void loop() {
}


the compiler complains about several errors:

Quote
MyTestLibrary\MyTestLibrary.cpp.o: In function `MyTestLibrary::storeNodeAddress()':
c:\arduino-0022\libraries\MyTestLibrary/MyTestLibrary.cpp:24: undefined reference to `EEPROM'
c:\arduino-0022\libraries\MyTestLibrary/MyTestLibrary.cpp:24: undefined reference to `EEPROM'
c:\arduino-0022\libraries\MyTestLibrary/MyTestLibrary.cpp:24: undefined reference to `EEPROMClass::write(int, unsigned char)'
MyTestLibrary\MyTestLibrary.cpp.o: In function `MyTestLibrary::getNodeAddress()':
c:\arduino-0022\libraries\MyTestLibrary/MyTestLibrary.cpp:15: undefined reference to `EEPROM'
c:\arduino-0022\libraries\MyTestLibrary/MyTestLibrary.cpp:15: undefined reference to `EEPROM'
c:\arduino-0022\libraries\MyTestLibrary/MyTestLibrary.cpp:15: undefined reference to `EEPROMClass::read(int)'


The only solution so far is to include EEPROM also in my sketch...
Thanks!
lucadentella.it

PaulS

Quote
The only solution so far is to include EEPROM also in my sketch...

The only solution at all is to include EEPROM also in your sketch.

lucadentella

Thanks Paul,

I'm a bit curious: why, even if in my sketch I don't use EEPROM functions directly, I have to re-include it?
I usually works with c#, java... and if - in one of my libraries or classes - I need another one I have to include (or "use") it only in that file, not in my main program.
lucadentella.it

Brig

I've been there too... very annoying...

but, for me, the reason is that the compiler is not very efficient

PaulS

Quote
but, for me, the reason is that the compiler is not very efficient

It's not the compiler that is the culprit. The IDE decides WHAT gets compiled (and copied to the build folder), based on what is in the sketch. If the sketch includes a header file, that header file gets copied to the build folder. If not, the header file does not get copied, and will not be available to include.

All this happens BEFORE the compiler is invoked.

Quote
and if - in one of my libraries or classes - I need another one I have to include (or "use") it only in that file, not in my main program.

Right. Completely different mechanisms for determining what needs to be compiled, and where the stuff is during the compile/link process.

Remember, the Arduino produces object files, but discards them when the build is complete. Your PC does not do that.

Go Up