Pages: [1]   Go Down
Author Topic: Using system library (EEPROM) in my custom one  (Read 1006 times)
0 Members and 1 Guest are viewing this topic.
Bergamo
Offline Offline
God Member
*****
Karma: 1
Posts: 663
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

lucadentella.it

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Bergamo
Offline Offline
God Member
*****
Karma: 1
Posts: 663
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

lucadentella.it

Massa, Italy
Offline Offline
Full Member
***
Karma: 0
Posts: 172
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Massa, Italy
Offline Offline
Full Member
***
Karma: 0
Posts: 172
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All this happens BEFORE the compiler is invoked

right! I forgot the "pre" smiley-razz
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Bergamo
Offline Offline
God Member
*****
Karma: 1
Posts: 663
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

lucadentella.it

Pages: [1]   Go Up
Jump to: