I have a file (call it file 1) wich includes a file for a class it needs (call that file File 2).
File 1 also includes another file, call this one File 3 (through another file - incase that matters).
File 3 also include File 2 for a class it needs, wich means that File 2 is included by both File 1 and File 3.
So dev c++ thinks File 2 is there twice, so it ends up with multiple defintions of everything in File 2.
Theres pretty much nothing else in the compiler log to say.
How can i fix this with the class neaded in File 1 and File 2 in File 3? Or is it impossible alltogether? I dont know much about the linker.
Oh and sry if i forgot something, this forum apears to have lots of rules.
Thanks in advance, Dragon 257.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
LOL, and one of the rules is always post your compile log and never assume that it contains nothing useful. :)
I'm not quite sure I follow your question, but it sounds like you're including a file twice, which is a pretty simple thing to fix. Put all the stuff you don't want to be double-included inside this sort of statement:
ifndef MY_HEADER_FILENAME_GOES_HERE
#define MY_HEADER_FILENAME_GOES_HERE 1
// stuff goes here
endif
Or something like that, I'm not sure about the exact syntax but I think that's close. The first time the compiler reads the file, MY_HEADER_FILENAME_GOES_HERE will be undefined and it will define it then process your code. On all subsequent reads of the included file it will see that MY_HEADER_FILENAME_GOES_HERE is already defined and skip down to the endif line.
Is that what you're looking for?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I thought of that but diden't think it would work, I thought that surely if it was like that then it would be included in either file but not both...
So does it just asure the linker/compiler that the linked file has allready been checked for compiler errors and all it needs to do is add the definitions and declerations?
Or is it that the compiler only needs it included in one place but if its used in another without the include line (no matter if its in a ifndef/ifdef) it throws a compiler error?
Thanks again, Dragon 257.
Oh and sorry for my inept writing skills.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Each thing it compiles needs whatever is defined in your header file to be read exactly once. I'm pretty sure that all happens before linking.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-09
>> So does it just asure the linker/compiler that the linked file has allready
>> been checked for compiler errors and all it needs to do is add the
>> definitions and declerations?
It has nothing to do with the linker, multiple definitions cause linker errors when teh definitionsoccur in multiple compilation units. You should note in the compile log that the build failed long before it got to linking.
You need to understand how the whole pre-process - compile - link process works.
The compiler only works on a single 'compilation unit' to create a single object file. A compilation unit is created by the pre-procesor. Pre-processor directives are any line beginning with a #. In the case of an #include, teh text of the referenced file is simply inserted into the compilation unit. So say you have three files:
because when the second #include "fileA.h" is encountered, FILE_A_INC has been previously defined, so teh content is excluded.
These "include guards" allow header files to be nested and included in any combination without causing errors. Take a look at the standard header files for example, you will see similar constructs.
Using Windows xp and dec c++ 4.9.9.2.
I have a file (call it file 1) wich includes a file for a class it needs (call that file File 2).
File 1 also includes another file, call this one File 3 (through another file - incase that matters).
File 3 also include File 2 for a class it needs, wich means that File 2 is included by both File 1 and File 3.
So dev c++ thinks File 2 is there twice, so it ends up with multiple defintions of everything in File 2.
Theres pretty much nothing else in the compiler log to say.
How can i fix this with the class neaded in File 1 and File 2 in File 3? Or is it impossible alltogether? I dont know much about the linker.
Oh and sry if i forgot something, this forum apears to have lots of rules.
Thanks in advance, Dragon 257.
LOL, and one of the rules is always post your compile log and never assume that it contains nothing useful. :)
I'm not quite sure I follow your question, but it sounds like you're including a file twice, which is a pretty simple thing to fix. Put all the stuff you don't want to be double-included inside this sort of statement:
ifndef MY_HEADER_FILENAME_GOES_HERE
#define MY_HEADER_FILENAME_GOES_HERE 1
// stuff goes here
endif
Or something like that, I'm not sure about the exact syntax but I think that's close. The first time the compiler reads the file, MY_HEADER_FILENAME_GOES_HERE will be undefined and it will define it then process your code. On all subsequent reads of the included file it will see that MY_HEADER_FILENAME_GOES_HERE is already defined and skip down to the endif line.
Is that what you're looking for?
Yay! It worked :)
I thought of that but diden't think it would work, I thought that surely if it was like that then it would be included in either file but not both...
So does it just asure the linker/compiler that the linked file has allready been checked for compiler errors and all it needs to do is add the definitions and declerations?
Or is it that the compiler only needs it included in one place but if its used in another without the include line (no matter if its in a ifndef/ifdef) it throws a compiler error?
Thanks again, Dragon 257.
Oh and sorry for my inept writing skills.
Each thing it compiles needs whatever is defined in your header file to be read exactly once. I'm pretty sure that all happens before linking.
>> So does it just asure the linker/compiler that the linked file has allready
>> been checked for compiler errors and all it needs to do is add the
>> definitions and declerations?
It has nothing to do with the linker, multiple definitions cause linker errors when teh definitionsoccur in multiple compilation units. You should note in the compile log that the build failed long before it got to linking.
You need to understand how the whole pre-process - compile - link process works.
The compiler only works on a single 'compilation unit' to create a single object file. A compilation unit is created by the pre-procesor. Pre-processor directives are any line beginning with a #. In the case of an #include, teh text of the referenced file is simply inserted into the compilation unit. So say you have three files:
fileA.h
define MACRO_A 255
extern void functionA() ;
fileB.h
include "fileA.h"
extern void functionB() ;
fileC.cpp
include "fileA.h"
include "fileB.h"
The fileC.cpp compilation unit looks like this:
define MACRO_A 255
extern void functionA() ;
define MACRO_A 255
extern void functionA() ;
extern void functionB() ;
The second declarations of functionA and MACRO_A occur because of the inclusion in fileB.h
Now consider these changes:
fileA.h
if !defined FILEA_INC
define FILEA_INC
define MACRO_A 255
extern void functionA() ;
endif
fileB.h
if !defined FILEB_INC
define FILEB_INC
include "fileA.h"
extern void functionB() ;
endif
Now the compilation unit has:
define MACRO_A 255
extern void functionA() ;
extern void functionB() ;
because when the second #include "fileA.h" is encountered, FILE_A_INC has been previously defined, so teh content is excluded.
These "include guards" allow header files to be nested and included in any combination without causing errors. Take a look at the standard header files for example, you will see similar constructs.
A perhaps more eloquent explaination can be found at http://en.wikipedia.org/wiki/Include_guard , and I wrote about the whole build process at http://sourceforge.net/forum/message.php?msg_id=2670009
Clifford
Ahhhhhh now i get it (i think), thanks alot :)
Dragon 257