AGHW-Arduino - Generic cpp and h files... ShareActions ! Actions ! ???????????

Hello,

I want to move some functions in an other cpp file and some declarations in a another h file, in order to use them in several pde files (arduino source)…

AGGen.h file:
#ifndef AGGen_h
#define AGGen_h

#include “WProgram.h”

#define AG_CONFIG_ID “a@g” // ID of the settings block
#define AG_CONFIG_Ver 1 // 0…255 with the version of the config structure
#define AG_CONFIG_START 0 // Tell it where to store your config data in EEPROM

struct AG_ConfigStruct { // AG settings structure
char id[4]; // This is for mere detection if they are your settings
byte ver;
char len;
} AG_Config = {AG_CONFIG_ID, AG_CONFIG_Ver,32};

#endif

AGGen.cpp file:
#include “WProgram.h”
#include <AGGen.h>
#include <EEPROM.h>

bool AG_LoadConfig_from_EEPROM() {
// To make sure there are settings, and they are YOURS!
// If nothing is found it will use the default settings.
if (EEPROM.read(AG_CONFIG_START + 0) == AG_CONFIG_ID[0] &&
EEPROM.read(AG_CONFIG_START + 1) == AG_CONFIG_ID[1] &&
EEPROM.read(AG_CONFIG_START + 2) == AG_CONFIG_ID[2]) {
for (unsigned int t=0; t<sizeof(AG_Config); t++)
((char)&AG_Config + t) = EEPROM.read(AG_CONFIG_START + t);
Serial.println(“loadConfig…”);
Serial.print(“storage.len: “);
Serial.print(AG_Config.len, DEC);
Serial.println(”----------”);
return true;
}
return false;
}

void AG_LoadConfig() {
// To make sure there are settings, and they are YOURS!
// If nothing is found it will use the default settings.
if (not AG_LoadConfig_from_EEPROM()){
AG_SaveConfig();
AG_LoadConfig_from_EEPROM();
}
}

In pde file:
#include <AGGen.h>

void setup () {
Serial.begin(57600);

AG_LoadConfig();
}

Whan compile, I get the message:
JeeTest_EEPROM.cpp: In function ‘void setup()’:
JeeTest_EEPROM:189: error: ‘AG_LoadConfig’ was not declared in this scope

If I add the declaration to AGGen.h:
#ifndef AGGen_h
#define AGGen_h

#include “WProgram.h”

#define AG_CONFIG_ID “a@g” // ID of the settings block
#define AG_CONFIG_Ver 1 // 0…255 with the version of the config structure
#define AG_CONFIG_START 0 // Tell it where to store your config data in EEPROM

struct AG_ConfigStruct { // AG settings structure
char id[4]; // This is for mere detection if they are your settings
byte ver;
char len;
} AG_Config = {AG_CONFIG_ID, AG_CONFIG_Ver,32};

void AG_LoadConfig();

#endif

compiler show:
AGLib\AGGen.cpp.o:(.data.AG_Config+0x0): multiple definition of `AG_Config’
JeeTest_EEPROM.cpp.o:(.data.AG_Config+0x0): first defined here

Can someone tell me how can I do this? How can I transfer some code in an other cpp/h file…? and use it in several pde files?

What am I doing wrong in above code?

Thank you

The header files need to contain the function prototypes. The proper format for the function prototype is as follows:

return_type function_name(paramtype1, paramtype2,...);

Param names are not required in the prototype, but it's ok to include them if you want. IE they are optional. Notice however, that the function prototype is terminated with a ; and there are no braces in the prototype (ie no { or }).

Actually this was a typing mistake by me…

In h file the function prototype is
void AG_LoadConfig();
and not
void AG_LoadConfig() {
(I fixed in first post…)

I do not know why I am taking this message…

I do not know why I am taking this message...

In your header file, you are defining a structure, [u]and creating an instance of that structure[/u].

Each time you use the header file, in a separate source file, you are creating another (global) instance of that structure with the same name.

The linker doesn't like (and won't) include global variables of the same name from different object files.

You need to stop creating an instance of the structure in the header file, and do that in one of the source files, so that only one instance gets created.

You are correct! This must be the problem...! I will check it tonight...

Thank you

Where is it appropriate to create the "AG_ConfigStruct" structure? In "AGGen.cpp" or in each pde file which uses (includes) AGGen.h?

Where is it appropriate to create the "AG_ConfigStruct" structure? In "AGGen.cpp" or in each pde file which uses (includes) AGGen.h?

You only want one instance, so, create that instance in ACGen.cpp, and reference it anywhere you need it.

In the sketch(es), use extern AG_ConfigStruct instanceName; where instanceName is the name of the instance created in ACGen.cpp.

Great! Thank you!