Link to home
Start Free TrialLog in
Avatar of Nusrat Nuriyev
Nusrat NuriyevFlag for Azerbaijan

asked on

Understanding of redefinition compiler error and multiple definition error.

My question is about to learn some preventive knowledges about redefinition/multiple definition compiler error.
Actually, when I get them - really get lost.
Also what about object files, executable and a bit more information about what does linker.
The good answer is those which is able to visually demonstrate the job of linker.
I know that, linker gathers objects together, and creates an executable. But that level of knowledge does not satisfy me.
It's too high level of abstraction. Need to see whole picture, but see in details.

Assuming that we are spearking in context of C programming language in Linux environment or Windows environment.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nusrat Nuriyev

ASKER

We can also make preprocessing w/o compilation by putting -E switch

g++ -E b.cpp

# 1 "b.cpp"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "b.cpp"
# 1 "a.h" 1


int f()
{

}
# 2 "b.cpp" 2
# 1 "b.h" 1



# 1 "a.h" 1


int f()
{

}
# 5 "b.h" 2
# 3 "b.cpp" 2

Open in new window


Could you explain a little bit about the content of this output. What does all lines with # sign mean?
1.
if you compile b.cpp the compiler would complain that function f has multiple definition because implementation (definition) of f was included twice by including a.h both in b.cpp and in b.h.

if b.cpp only would include b.h the compiler is happy. but if you include a.h or b.h in a.cpp, the linker would complain because it finds the same 'object' code for function f in b.obj (b.o in linux) and in a.obj.
Could we say, that there are two "levels" of redefinitions: at source code level and at object code level?

2. Well, I have tried mutual inclusion, g++ compiler falls into an infinite loop. :)

3. Could you provide an example of "same 'object' code for function" compiler error?

4.
implementation must be provided by include files such that the compiler can create machine code for the concrete template type(s) you added in your source code. anyway, finally the object file contains all machine code from implementation of your source code.
So, when I add
 #include "stdio.h"

Open in new window

which object file is embedded to my object file?

5. Can we say that object file == executable file with one exception that object file must not contain main function?
Could we say, that there are two "levels" of redefinitions: at source code level and at object code level?
actually it is more a duplicate than a redefinition. the errors occur because either the compiler or the linker detect an ambiguous definition which they can't resolve. obviously the compiler can detect ambiguous definitions only in the source file it was compiling and the code of header files which have been included by the precompiler.

Well, I have tried mutual inclusion, g++ compiler falls into an infinite loop. :)
bad. the precompiler should have a maximum include depth.

Could you provide an example of "same 'object' code for function" compiler error?

class X
{
      void f();
};

void X::f()
{
    ...
}

void X::f()
{
    ...
}

Open in new window


the compiler error is 'X::f already has a body.'

Sara