last day (15 days later) » 

9:26 PM
0
Q: atmel studio ifndef compilation error

kyrpavHi 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 de...

 
Are you sure you didn't mean to define that as a type?
 
as u see i just declare a certain struck of type letterstruct this is all that i do.The struck is defined at the letterDefinition.h
 
And then you declare a variable of that type called "Alphaletter".
 
i declare and then i initiate the alphaletter and in the test function i try to check the variable alphaletter.name
 
Yes, and you declare it in two different compilation units.
 
9:26 PM
why do you say that.if i type int favNumber=5 in one header and then go to another header , include the first one and che if(favNumber==10){//and do something} why do i declare it twice
 
Headers are not compilation units.
 
sorry i did not mean go to another header
i mean to the implementation of a function ofcourse at the cpp file
Why do you think this is declared in 2 different positions
 
Because that's what the error is telling you.
That error is explicitly telling you that the same variable has been declared multiple times.
 
yes but i can not understand why is this the error since what i do is this:
i declare 2 structs
i pute them in an array
aput*
 
The code in your question does not only define a struct.
It also declares a variable.
 
9:31 PM
yes
i declare not only 1 but 2 variables
i initiate them
and i add them to an array
these all happen in the same letterdefinition.h
 
I have no clue what this array thing is. It's not in the posted code, and it's not actually relevant to the issue.
 
the i go to my class and at the test function i check if a certain struct exist
i am trying ot explain that what i do is. declaring and initiating one struck in one header
 
The error is caused strictly by the code posted in the question.
 
and then at another class
 
Yes, and doing just that is what is causing the error.
Because the same variable is being declared in two different compilation units.
 
9:34 PM
i check a variable o that struck to see if it has a certain value
i am sorry declare means Struct test={ .......}
if (test.name =="jim"){//do something} is not adeclaration
declare and initiate
 
Okay, let's try another tack...
 
sorry i did not type ity correctly but i think you get the point
 
Which .cpp files eventually include LettersDefinition.h?
 
struct t{...} test;
i mean
ledcontroller.h and .cpp
 
Not headers. Strictly .cpp files.
 
9:38 PM
ledcontroller.cpp
 
What about LedMatrix7219.cpp?
 
i call the function that is in ledcontroller.cpp there
ledMartix7219.cpp has the main arduino program
setup and loop functions
 
Are you saying that LedMatrix7219.cpp never includes LettersDefinition.h?
 
nope
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include "LedController.h"
 
And you're saying that LedController.h never includes LettersDefinition.h?
 
9:41 PM
it does cause i need one more struck at the ledcontroller class
i have inlude it at both header and cpp cause if i don't i can not use the struct
but this is ifndef is for
 
So then LettersDefinition.h is included by both LedController.cpp and LedMatrix7219.cpp.
 
in order not to reinclude headers
 
And it works perfectly. The header is never included more than once in the same compilation unit.
 
ledMatrix7219.cpp as you see above and to my post includes the header of the class LedController.h
Ledcontroller.h and ledcontroller.cpp include both of the the leddefinition.h
both of them
1 ledcontroller.h includes leddefinition.h cause i declare another struck of the same type with another name.
and
2 ledcontroller.cpp has the function test that ckeck the variable .name of the Alphaletter struck that you see in my question
these are different things
 
And neither of those is actually relevant to the problem.
You have AlphaLetter declared in two different compilation units.
 
9:46 PM
yes this is whati t says
 
And you can't do that.
You need to move the declaration into a single compilation unit.
 
why do yo usay declare
what do you mean about declare
 
<type> foo; declares a variable called "foo" with the given type.
 
alphaletter name is typed only in letterdefinition.h
nowhare elss
ok
continue plz
 
Yes, which is included in two compilation units.
It is included in both LedController.cpp and LedMatrix7219.cpp.
 
9:50 PM
what are the 2 compilation units? ledcontroller.cpp and ledmatrix7219.cpp?
 
Technically LedController.cpp.o and LedMatrix7219.cpp.o, but yes.
 
but the only thing i do is using a variable declared outside of a class, inside a fuction of that class
and then my main runs the function
 
Headers are included literally into files. Both .cpp files eventually contain "struct ... AlphaLetter".
 
i do not have typeofstruct Alphaletter twice
 
Yes, you do, due to the includes.
 
9:53 PM
but ifndef doesn't firbids this to happen?
cause if i do not includde the header i can not use the struct
 
Only within the same compilation unit.
The trick is to define the type in the header, and declare the variable in the .cpp.
 
so the problem is that i declared and the struct at the header and also defined/initiate the variable there?
 
"Defined" and "declared" are backwards in that sentence, but yes, that is exactly the problem.
 
so you are saying that i sould have a .h with only the letterStruck Alphaletter in it and at the .cpp file of the same header to
type that Struct letterstruck{....} and Alplaletter={....}
 
You need to define struct LetterStruct in the header, and declare struct LetterStruct AlphaLetter = ... in the .cpp.
 
9:59 PM
kk
but plz i am not capable to fully understand something obviousy
if i have something declared inside aheader this could not me included twice
 
Within the same compilation unit.
 
why this does not work for the definition
so if i have a variable int number=5 insode a header
and i include the header in 4 classes this would have been a problem?
 
Because the definition does not actually allocate space for anything; it simply tells the compiler that it should treat a certain type a certain way when it encounters it.
But a declaration does allocate space, and the linker will throw when it tries to resolve the symbols.
 
yes but this way i understand that if i have a class i can not add its header to 2 other .cpp of different classes
which i strongly believe it can be done
add= include
 
You can include any file wherever and whenever you like. But if the linker can't make sense of it then you won't be able to build it.
 
10:05 PM
the problem is also solve if i delete the include letterDefinition from the ledcontroler.h
 
Yes, because then only one of the compilation units contains the declaration.
 
which one?
 
The one that eventually includes the header that contains the declaration.
 
sorry but ledmartis7219.cpp includes arduino.h and all files does why this does not make any proble,
 
Arduino.h does not contain declarations.
 
10:08 PM
ledmatrix7219.cpp -->ledcontroller.h-->ledcontroller.cpp-->leddefinition.h
 
Before we continue, I need to apologize.
I just double checked and I have been using "declare" and "define" incorrectly.
I will start using them correctly from now.
 
can you correct something you said so i can understand?
 
struct TestLetter {...}; is a definition, as is struct TestLetter AlphaLetters;.
 
i can not follow why a variable delcared and initialized in a .h can not be used in another .cpp while the variable's header that declares a struct is included and in the same .h of the same .h
so define and declare is somewhat the same
 
Because a type definition tells the compiler what to do, and a variable definition tells the linker what to do.
The compiler treats each compilation unit as independent parts, and the linker combines them into a single executable.
So the compiler can be told the same thing over and over, but the linker can only be told once.
 
10:14 PM
ok and ifndef forbid the header to be read twice
 
By the compiler, within the same compilation unit.
 
so everything inside the header also
 
The linker does not see the compilation directives. It only deals with object files.
 
what you are sying not is that if i have classes foo1,foo2,foo3,foo4 i can not include the myheader.h into all of them
saying
 
You can include any file wherever and whenever you like.
But if the linker can't make sense of it then you won't be able to build it.
The linker does not see type definitions.
 
10:16 PM
i miss something
 
Type definitions only exist to tell the compiler where it needs to poke in order to get things done.
 
i know that everytime i include a header that is included at least one time i want be included again. is that correct?
a program can have 10000 .cpp files
 
Within the same compilation unit.
 
so a variable that is declared in that header can not be used in more than one .cpp file
 
More than one compilation unit.
 
10:19 PM
give me an expample plz
 
.cpp files do not always directly map to compilation units.
If a.cpp and b.cpp are compiled together (gcc a.cpp b.cpp -o ab.o) then they become a single compilation unit.
 
so if .cpp does not always directly map to compilation unit how do i know what to do
how do i know what is going to compiled with asomething else?
to be compiled
 
Examine the compilation command lines.
 
plz continue with the example
 
The Arduino IDE will always compile each .cpp into a separate compilation unit.
 
10:22 PM
so...?plz analyse
 
So if you have int foo; in a header included by both a.cpp and b.cpp, there won't be a problem since they are both in the same compilation unit.
But if you include that header in c.cpp, and then compile that to c.o, you won't be able to link that with ab.o since the same symbol (foo) will exist in multiple compilation units.
ld will complain with "multiple definition of foo".
 
i am sorrry yo usaid
The Arduino IDE will always compile each .cpp into a separate compilation unit.
and then
So if you have int foo; in a header included by both a.cpp and b.cpp, there won't be a problem since they are both in the same compilation unit.
same or saperate
 
"If a.cpp and b.cpp are compiled together (gcc a.cpp b.cpp -o ab.o) then they become a single compilation unit."
The IDE won't do this, but we certainly can.
brb
 
and if these are compiled together there is not going to be any problem.
so the compiler of the atmel makes .cpps in different compilation units
 
Sounds like it.
 
10:31 PM
so if i s int foo=5 in aheader i can not include the header into 2 .cpp if these cpp do not compile together?
 
If they're not compiled together but are later linked together.
 
linked together?
so if i initiate alphaletter in the letterdefinition.cpp and then i include the definition.h into the header and the cpp of another class isn;t it a multiple declaration?
 
letterdefinition.cpp is never included anywhere else, so only the single declaration exists.
 
the declaration happens in the header
and the header is included everywhare
 
Then don't declare it in the header.
 
10:35 PM
header declares variables
 
isn;t int foo; a declaration?
 
Yes. But why is that in the header?
 
i am sorry what should have been in a header except declarations
when you start a class you declare its variables at the header not at the cpp
 
What happens if we move the declaration into a.cpp?
 
10:41 PM
what do you mean
 
Let's move int foo; out of x.h (included by a.cpp, b.cpp, and c.cpp) and into a.cpp.
What happens to references to foo in a.cpp?
 
since you have placed it into a.cpp it exists there
but it does not in b.cpp
and you can not include the a.cpp into b.cpp
 
Excellent. I was hoping you'd catch that.
So then we need to tell the compiler that foo exists regardless.
 
of course
 
And we do so by using extern.
This tells the compiler that the symbol exists, but that the linker will deal with actually finding it.
 
10:45 PM
ok
 
So now we have extern int foo; in x.h.
And since c.cpp includes it, the compiler knows that foo exists somewhere.
So in the object file the compiler generates a reference to it.
And then the linker finds it in ab.o and links it.
 
ok
i know about extrern
 
So everyone knows about it, but only one compilation unit declares it.
 
i do not usually use it
and it is the first time i have this problem
so
i need to make something clear
we have not only a variable but 3 of them that are public in a class foo
and these are static
so we do not deal with different insances of that class
i understand that i have have error if i icnlude this class header to different classes cause thisw will give me multiple definitions
if i do not use extrern
but i do not see extrent variables everywhere in programs
 
Because it will exist in multiple compilation units that are being linked together.
Many programs consist of only a single compilation unit.
 
10:50 PM
but this is done to arduino projects that you have said that each cpp is a different compilation unit
so this would have been a problem
i have a heliostat project that works a machine that trully works with static variables that are used from different classes to make the machine work and i did not have that problem
i should have seen this error before
how can i check this compilation units in atmel studio?
 
No idea. I've never used it.
 
-funsigned-char -funsigned-bitfields -D__AVR_ATmega2560__ -DARDUINO=161 -DARDUINO_MAIN -DF_CPU=16000000L -D__AVR__ -I"c:/program files (x86)/arduino/hardware/tools/avr/lib/gcc/avr/4.8.1/include"
-I"c:/program files (x86)/arduino/hardware/tools/avr/avr" -I"c:/program files (x86)/arduino/hardware/tools/avr/avr/include/avr" -I"c:/program files (x86)/arduino/hardware/tools/avr/avr/include"
is this part of it?
i suppose?
 
Those are compiler flags. The filenames to be compiled will indicate what a compilation unit is, and it's none of those.
 
so since i have done this thing in arduino ide why i did not get anyerrors?
also i get another error. i have inititiate 2 variables of the struct and also declared and initiate an array that adds them
my test function does not recognise the array
cause it is not known
 
The code you type into the Arduino IDE is not the code it compiles. The IDE hides a lot from the developer.
 
11:02 PM
nice
morer problems
LetterStruct Alphaletter = {Alpha,5,
6,2,2,2,6,
2,3,4,5,6,7,
2,5,
2,5,
2,5,
2,3,4,5,6,7
};

LetterStruct Alphaletter2 = {Beta,5,
6,2,2,2,6,
1,2,4,5,6,7,
1,5,
1,5,
1,5,
1,3,4,5,6,7
};

arr[]={Alphaletter,Alphaletter2};
#ifndef LETTERSDEFINITION_H_
#define LETTERSDEFINITION_H_

#include "arduino.h" # 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];
};

struct LetterStruct arr[];
sorry for the miss type
infdef for the definition.h happens once only
the initiation of the allphaletter happens to that .cpp file
 
I need to step away for a few minutes, but I'll continue helping you after.
 
kk
 
11:20 PM
i will go to rest for a few hours
i will try to find the solution later and i will post you again if i need any help
 
Okay, where were we... reads up
 
well
plz
 
That code declares arr in the header, leading to multiple definitions of arr.
 
read the header and the ccp file
 
You need to declare it in the .cpp and change the header to use extern.
 
11:26 PM
if i declare it in the .cpp
then the file tledcontroller can not read it
 
We use extern to tell the compiler about it, and have the linker actually resolve the symbol.
 
at the declaration at the .cpp file
extern LetterStruct arr[]={Alphaletter,Alphaletter2};
LedController.cpp:In member function 'void LedController::test()'
LedController.cpp:51:19: error: 'arr' was not declared in this scope
 
The .cpp needs to have the actual declaration. It should not use extern.
We use extern in the header to tell other compilation units about the symbol.
 
kk
can you tell me about the classes in general
i have assked you before
?
 
What did you want to know?
Classes are a type with code. We declare the type in the header, and the code in the .cpp file.
 
11:30 PM
one class with with 4 static variables
that i need to read in 4 different classes
so i have to include them to their cpp files
i mean the header to the .cpp files
 
I'd need to see a concrete example of that, but it doesn't sound like good design to me.
 
wont i have the same problem?
 
Class members are part of the type, not a variable.
 
static variables
 
Only the instance variable itself needs to be declared once.
static members*
 
11:32 PM
lets stop there
not static
variable instance need to be declared once
so int foo; need to be declared once
and if i want to gice a number
i can not do it like int foo=5 into the header
 
class foo
{
private:
int bar;
};

is a type definition. `foo::bar` doesn't actually exist anywhere.
 
lets forget about classes and static variables
please focus on what i have typed before
 
You would not have int foo = 5; in the header, no.
 
so i can not initiate the foo at header cause
 
The header would have extern int foo;.
One of your compilation units would have int foo = 5;.
 
11:35 PM
if a add the header in 5 different cpps i will have the same problem?
but
but but
when cpp find the #include myheader.h line
 
foo would be added to the symbol table.
 
is should not be included twice normally ( but i suppose that here you will tell me that if it not the same compilation unit it will include it again)
 
Correct, each compilation unit will include it separately.
 
and then the linker will give me the error
is that correct?
 
No, it won't. foo only actually exists in one of the units.
 
11:38 PM
ok sorry
 
It will be an external reference in the other units.
 
so since the foo will be included in more than one compilation untis
what is the real problem
why does this not work since the linker know s that this is one and the same variable
 
The real problem is that you cannot have multiple units declaring a non-external symbol of the same name.
 
how does the compilation units conflict each other
 
There will be a conflict since multiple symbol tables will have the same non-external symbol.
 
11:40 PM
so compile check the units for same name variables
 
No, linking.
 
and give error
so the linking
not the linker
 
The linker is what attempts to resolve all symbols in all compilation units.
 
and linking is the prosses
 
Correct.
Compiling and linking are separate phases of building.
 
11:42 PM
so why you said that the linked
the linker does not give me the error
 
Because all but one of the units will have the symbol declared external.
 
the linker while it links the symbols should not find the same variable in different compilation units
 
The linker will see this and attempt to connect the external references to the real symbol.
 
how gives the error?
who
 
If the same non-external symbol exists in multiple units, the linker will throw a multiple definition error.
 
11:44 PM
if i have multiple declaration, who does give the error the compiler or the linker
 
"ld returned 1 exit status"
And ld is the linker.
 
and then the linker will give me the error
this is what i have written before
if you check
 
Multiple declarations in the same unit are caught by the compiler. Multiple declarations in different units are caught by the linker.
 
No, it won't. was your answer
kk
 
extern causes the compiler to emit a slightly different symbol, one that the linker will attempt to resolve to a real symbol somewhere else.
 
11:46 PM
i understood it know
but plz check your aswer to be clear at before 8 minutes
 
Which part?
 
i have told you that the comliler will include the variable into multiple compilation units and the linker will give me an error
and you said no it wont
compiler
 
I had mentioned changing the code so that the header had extern.
 
propably you where answering to something else
ok
kk
 
I think we crossed streams there.
 
11:49 PM
very helpfull
can i ask you something slightly different and it will be my last question
 
Absolutely.
 
if i want to add all my declared structs in to a progmem array (which is const) kai i make the declaration of the strucks inside the declaration+initializatoin of the array?
or i have to initiate all the structs outside the array and then add them to it ( cause this makes me believe that i need the the same memory twice one for the flash and one for the runtime
 
Since the array only contains pointers to the structs, you must declare them separately in order to put all of it into flash.
 
why the array contains pointers only
 
Because that's how arrays of non-arrays or variable arrays work.
If you had arrays of fixed arrays then you could define it as one, as the avr-libc documentation shows.
 
11:56 PM
PROGMEM prog_uchar CH[] = {
3, 8, B0000000, B0000000, B0000000, B0000000, B0000000, // space
1, 8, B1011111, B0000000, B0000000, B0000000, B0000000, // !
3, 8, B0000011, B0000000, B0000011, B0000000, B0000000,
these are not pointers
i did not add the whole array
 
That is a single array, not an array of structs.
 
kk i suppose i will find my answers at the site
 
Read the part "Storing and Retrieving Strings in the Program Space" carefully. Structs are in the same boat.
 
if i have to declare them seperately there is no need to add them into the flash cause i will have already lost program space
i will
 
You create the array in order to access them more easily, not to save space.
 

  last day (15 days later) »