Link Error 'Multiply Define Symbol'

R

Rafi Kfir

"Hi,

This is a very simple question that confuses me (probably due to some
lack of knowledge...)

I will illustrate my problem with a smiple example:


My project has the following files:


file1.c
#include file3.h


file2.c
#include file3.h


file3.h
#ifndef _file3
#define _file3


char a[4]; //this one causes no error
char b[]="Hello"; //this one causes the error


#endif


Can anybody explain why I'm getting the link error upon compilation.


Thanks"
 
V

Victor Bazarov

Rafi said:
This is a very simple question that confuses me (probably due to some
lack of knowledge...)

I will illustrate my problem with a smiple example:


My project has the following files:


file1.c
#include file3.h


file2.c
#include file3.h


file3.h
#ifndef _file3
#define _file3


char a[4]; //this one causes no error
char b[]="Hello"; //this one causes the error


#endif


Can anybody explain why I'm getting the link error upon compilation.

Because your symbol 'b' is defined in more than one compilation unit.

Rule: never put definitions of non-const data in headers.

Hint: double inclusion guards have no effect across compilation units.

V
 
J

John Harrison

Rafi Kfir said:
"Hi,

This is a very simple question that confuses me (probably due to some
lack of knowledge...)

I will illustrate my problem with a smiple example:


My project has the following files:


file1.c
#include file3.h


file2.c
#include file3.h


file3.h
#ifndef _file3
#define _file3


char a[4]; //this one causes no error

It should do
char b[]="Hello"; //this one causes the error


#endif


Can anybody explain why I'm getting the link error upon compilation.

Do it like this

// file3.h
extern char a[];
extern char b[];

// either file1.c or file2.c not both
char a[4]; //this one causes no error
char b[]="Hello"; //this one causes the error

You need to understand the difference between a declaration and a
definition. You can only have one definition. What you had before put the
definition in the header file. When you include that header file in more
that one .c file you get multiple definitions and linker errors.

The way I did it, you have a declaration in the header file, you can have as
many declarations as you like. But I put the definitions in one of the .c
files.

john
 
R

Rafi Kfir

Thanks Victor,
Hint: double inclusion guards have no effect across compilation units.

What is double inclusion guards? Can you explain with an example please?

Thanks
Rafi
 
V

Victor Bazarov

Rafi said:
Thanks Victor,




What is double inclusion guards? Can you explain with an example please?


It's that stuff that you have in your header in the beginning and at
the end:

#ifndef _file3
#define _file3

....

#endif

V
 

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,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top