Linker error multiple definitions

  • Follow


Hello,

I am recieving a multiple definition error from the linker when I try
to build a project I am working on. The message states that the
functions I defined within an external .c source file are already
defined in the main program .obj file. I declared the functions in
seperate header files and used the pre-processor conditional #ifndef
FILE_H to make sure each header is only included once. Each .c source
file includes the associated header as does the main .c file which
calls the functions. I know I have done this before without any
problems, but am I missing a step? Thanks for any help.
0
Reply andrew.zdenek (14) 11/18/2003 4:17:19 PM

Andrew wrote:

> I am receiving a multiple definition error from the linker
> when I try to build a project I am working on.
> The message states that the functions I that defined
> within an external .c source file are already defined
> in the main program .obj file.
> I declared the functions in separate  header files

Are you sure that you didn't actually *define* the function
in the header files?

> and used the pre-processor conditional #ifndef FILE_H
> to make sure each header is only included once.

But your compiler isn't complaining about multiple *declarations*.
It is complaining about multiple *definitions*.
You can *declare* a function multiple times
but it should only be *defined* once.

> Each .c source file includes the associated header as does the
> main .c file which calls the functions. I know I have done this before
> without any problems, but am I missing a step? Thanks for any help.


0
Reply E.Robert.Tisdale (2031) 11/18/2003 4:29:53 PM


andrew.zdenek@ca.com (Andrew) wrote:

> I am recieving a multiple definition error from the linker when I try
> to build a project I am working on. The message states that the
> functions I defined within an external .c source file are already
> defined in the main program .obj file. I declared the functions in
> seperate header files and used the pre-processor conditional #ifndef
> FILE_H to make sure each header is only included once.

Only once in each .c file you #include it in, not only once per project.

> Each .c source
> file includes the associated header as does the main .c file which
> calls the functions.

Don't _define_ objects in headers. _Declare_ objects in headers, then
define them - once - in an appropriate .c file.

Richard
0
Reply rlb (4118) 11/18/2003 4:40:02 PM

Andrew wrote:

> I am recieving a multiple definition error from the linker when I try
> to build a project I am working on. (...)

Maybe we can help if you show us the exact error message(s) for one
symbol, as well as the .h source snippet and the .c file snippet which
declares the corresponding symbol.

-- 
Hallvard
0
Reply Hallvard 11/18/2003 4:43:31 PM

"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in message news:<3FBA4901.5000806@jpl.nasa.gov>...
> Andrew wrote:
> 
> > I am receiving a multiple definition error from the linker
> > when I try to build a project I am working on.
> > The message states that the functions I that defined
> > within an external .c source file are already defined
> > in the main program .obj file.
> > I declared the functions in separate  header files
> 
> Are you sure that you didn't actually *define* the function
> in the header files?
> 
> > and used the pre-processor conditional #ifndef FILE_H
> > to make sure each header is only included once.
> 
> But your compiler isn't complaining about multiple *declarations*.
> It is complaining about multiple *definitions*.
> You can *declare* a function multiple times
> but it should only be *defined* once.
> 
> > Each .c source file includes the associated header as does the
> > main .c file which calls the functions. I know I have done this before
> > without any problems, but am I missing a step? Thanks for any help.

Thanks everyone. I think I found the problem. It was not actually the
functions that the errors were citing, but some arrays which I
declared and initiliazed within the header files such as

int riff1[11] = {7,9,10,12,14,12,7,9,5,7,5};

Now that I moved these arrays to the .c source file from the header
everything is working again. I guess I was re-initializing or
re-defining these arrays each time I included the headers. Thanks
again
0
Reply andrew.zdenek (14) 11/19/2003 3:57:09 PM

4 Replies
40 Views

(page loaded in 0.206 seconds)


Reply: