Using system library (EEPROM) in my custom one

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:

#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
#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:

#include <MyTestLibrary.h>

MyTestLibrary myTestLibrary;

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

void loop() {
}

the compiler complains about several errors:

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!

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.

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.

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

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

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.

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.

PaulS: All this happens BEFORE the compiler is invoked

right! I forgot the "pre" :P

right! I forgot the "pre"

It happens before any tools in the compile/link tool chain get invoked.

On a Unix system, for example, one creates make files for a project of any complexity. The make file defines the rules for mapping one kind of object to another (source to object, object to library, library to executable, etc.) with multiple rules for various kinds of objects (source - .c, .cpp, .f, .s, etc., object - .o, etc.). The make file also contains a list of files to consider - all the source files that make up a library, for instance.

It is this make file (information) that the Arduino IDE is constructing (and that there are some issues with). Once the make file is constructed, make does whatever is required to map the (modified) input files to the specific output files. If it determines that the process of building a .o file requires a specific .c file, and that .c file is newer that the .o file, then it knows it needs to apply the .c to .o file rule to the .c file.

There determination of what files are relevant to the process is one of the week points of the whole build process.

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

Thanks Paul, your explanation is very clear!