By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Linker error multiple definitions

P: n/a
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.
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
an***********@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
Nov 13 '05 #2

P: n/a
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
Nov 13 '05 #3

P: n/a
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.

Nov 13 '05 #4

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<3F**************@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
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.