kyrpav
kyrpav

Reputation: 778

atmel studio ifndef compilation error

Hi imy project recognises double definitions of variables that do not exist 2 times. I suppose that some how by changing my code and recompiling it stucks.

LedMatrix7219.cpp.o:(.data.Alphaletter+0x0): multiple definition of `Alphaletter' LedController.cpp.o:(.data.Alphaletter+0x0): first defined here

LedMatrix7219.cpp.o:In function `loop' LedController.cpp.o:(.bss.arr+0x0): first defined here

LedMatrix7219.cpp.o:In function `loop' LedController.cpp.o:(.data.Alphaletter2+0x0): first defined here

collect2.exe*:error: ld returned 1 exit status

I have a class LedController and a header LettersDefinition.h

All the headers start like this:

I am including a struct and an enum from the LetterDefinition.h to the LedController so at the header i need to include the LetterDefinition.h in order to make a certain struck.

#ifndef __LEDCONTROLLER_H__
#define __LEDCONTROLLER_H__

#include <Arduino.h>
#include "LettersDefinition.h"

LetterStruct finalText;
String theText="Test";

void test();
//it does some extra staff
#endif //__LEDCONTROLLER_H__

And the header of the letter definition.

#ifndef LETTERSDEFINITION_H_
#define LETTERSDEFINITION_H_

#include "arduino.h"
#include <avr/pgmspace.h>

struct LetterStruct{

    lettersEnum name;
    uint8_t size;
    uint8_t columnSize[5];
    uint8_t data[18];
}Alphaletter;
#endif /* LETTERSDEFINITION_H_ */

And from my main .ide file i call the test function of the Ledcontroller i a get the error you see above. The test fuction just checks the LetterStruct.name variable nothing more.

My .ide is something like:

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include "LedController.h"   

LedController controller;

void setup()
{
    //irrelevant inits
}

void loop()
{
    controller.test();
    delay(2000);
}

If i delete the #include "LettersDefinition.h" from the LedController.h this error gives its place to an error that the LetterStruct is not defined in the LedController.h which is normal since i have to add the LettersDefinition.h in order to be defined.

Upvotes: 0

Views: 651

Answers (1)

jdr5ca
jdr5ca

Reputation: 2819

Your problem originates that you "define" variables in header files. This in general will lead to the multiple definition problem and is not standard design.

The model you need to follow is to define once in a source file:

//some.cpp
// this is define
int variableX = 5;

And declare in the header file:

//some.h
// this is declare
extern int variableX;

Every other source file that includes the header just processes the "extern" line, which says roughly "there is an int variableX that will exist in the final program". The compiler runs over every .cpp .c file and creates a module. For the some.cpp that defines the variable, it will create the variableX. All the other .cpp files will just have the extern reference which is a placeholder. The linker will resolve those placeholders when it combines all the modules together.

In your specific case, this means changing:

// .h file should only be externs:
extern LetterStruct finalText;
extern String theText;

// .cpp file contains definitions
LetterStruct finalText;
String theText="Test";

Upvotes: 1

Related Questions