Linker error multiple definitions

A

Andrew

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.
 
E

E. Robert Tisdale

Andrew said:
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.
 
R

Richard Bos

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
 
H

Hallvard B Furuseth

Andrew said:
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.
 
A

Andrew

E. Robert Tisdale said:
Are you sure that you didn't actually *define* the function
in the header files?


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.

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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top