#include <Arduino.h>
#include <test.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.print("Test int is ");
Serial.println(t_int);
}
void loop() {
// put your main code here, to run repeatedly:
Get this error:
Test\test.cpp.o:(.bss.t_int+0x0): multiple definition of `t_int'
sketch_test.cpp.o:(.bss.t_int+0x0): first defined here
d:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.8.1/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
collect2.exe: error: ld returned 1 exit status
Error compiling.
I have looked for answers online but they were no help. Why is it defining t_int twice when I used the #ifndef#endif tags?
When text.cpp is compiled, test.o is produced, and it contains a definition for the variable t_int. When the sketch is compiled, it contains a definition for t_int. When the linker tries to link the sketch's .o file and test.o, there are two definitions for t_int, and it doesn't know which one to use.
PaulS:
When text.cpp is compiled, test.o is produced, and it contains a definition for the variable t_int. When the sketch is compiled, it contains a definition for t_int. When the linker tries to link the sketch's .o file and test.o, there are two definitions for t_int, and it doesn't know which one to use.
If text.cpp contains a definition for t_int because of the #include "test.h" statement, then why doesn't the morse example also contain multiple definitions? Is it because they are class definitions? Should variables be defined in .cpp files only? The sketch.ino doesn't have a definition for t_int.
What is the correct procedure if I want to define the variable t_int in the header file and initialize it to a particular value in the .cpp?
I am trying to follow convention but I am tempted just ignore the .cpp because everything in the header file is copied anyway. Why should I bother with .cpp if all it produces is unnecessary complications? I have done manual compiling and linking of (C) object files in a programming course but I could never figure out how to set the main entry point i.e global.main.
After some research on the extern keyword I see its a way of defining (but not initializing) global variables. Now I am wondering why variables declared inside a sketch does not require the extern keyword. Does a sketch have its own "global" scope?
syphex:
After some research on the extern keyword I see its a way of defining...
"Declare" not "define". There is a difference. Which is especially important when searching / reading about such things.
Now I am wondering why variables declared inside a sketch does not require the extern keyword.
Because they are "defined" in the sketch.
Does a sketch have its own "global" scope?
No.
Defining tells the compiler about type and name but does not indicate storage.
Declaring does all three: type, name, and storage.
There can be only one memory location for a given global data. Which means there can be only one storage declared for a given global data. One declaration. Any number of definitions. One without "extern". Any number with "extern".
Sorry for mixing up the terminology. So header files can only contain declarations and .cpp files can only contain definitions? What are the exceptions to this?
edit: If I want functions external to a class do I need to declare them extern in the header file and define them in the .cpp?
syphex:
So header files can only contain declarations and .cpp files can only contain definitions?
No. Either can contain whatever you want. From the compiler's perspective it is one stream of text.
However, as I already said, there can be only one storage declared for a given global data. So, if a header file contains a declaration that header file can only be included in one compilation unit (one dot-cpp file). If that header file is included in more than one compilation unit then the linker complains about the multiple declarations.
What are the exceptions to this?
See above.
edit: If I want functions external to a class do I need to declare them extern in the header file and define them in the .cpp?
extern is optional for functions.
The declaration of a function has a special name: function prototype.
Like global data, function declarations and definitions can be anywhere. Like global data, function definitions can only appear in a single compilation unit (either directly or through an include).