C linker error

F

franco ziade

I have this situation

file1.h
{ define struct ... B;
}

file2.c
{ include file1.h
make use of B}

file2.c
{ include file1.h
make use of B .. }

It compiles ok however when it tries to link, It gives the famous error
Struct B : warning, multiply defined symbol

Can anyone help on what i should be looking for ?
I don't see anything wrong in the code ..

Thanks
 
W

Walter Roberson

:I have this situation

:file1.h
:{ define struct ... B;
:}

:file2.c
:{ include file1.h
:make use of B}

:file2.c
:{ include file1.h
:make use of B .. }

:It compiles ok however when it tries to link, It gives the famous error
:Struct B : warning, multiply defined symbol

If you are linking file2.c in twice to the same executable, it is not
surprising that you are getting duplicate symbols.

I will presume here that you mean file1.c in the first inclusion,
rather than file2.c.


:Can anyone help on what i should be looking for ?
:I don't see anything wrong in the code ..

If file1.h defines storage for B, rather than just declaring a type
for B, then when you include it in file1.c you are defining that storage
as being part of file1.o, and when you include file1.h in file2.c
you are defining that storage as being part of file2.o. Link the two
together and of course your symbol is multiply defined.

In file1.h you should only declare the type of B, and possibly have
an 'extern' reference to it. Then in -one- place, define it's storage.
 
J

Jack Klein

I have this situation

file1.h
{ define struct ... B;
}

file2.c
{ include file1.h
make use of B}

file2.c
{ include file1.h
make use of B .. }

It compiles ok however when it tries to link, It gives the famous error
Struct B : warning, multiply defined symbol

Can anyone help on what i should be looking for ?
I don't see anything wrong in the code ..

Thanks

Show the real code from file1.h. All references to the structure
type. Nobody can tell for sure what your mistake is from the non C
text that you posted.
 
J

Jogi Kuenstner

franco said:
I am sorry .. the third file is file3.c
Now it is corrected below

But the explanation still is the same:
If you are _defining_ storage in your header-file and then including
this headerfile into two different c-files, you are actually defining the
same stuff two times, once in file2.c and once in file3.c
(include is a textual include, so everything in file1.h becomes part of
file2.c AND file3.c)

That's exactly what you have to do to oevrcome the error.

HTH
Jogi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top