Log in

View Full Version : Linker Error


Max Lobovsky
17-05-2004, 14:41
I get the following error whenever i try to compile FRCcode with any variables declared in user_routines.h (in this case, "int bob;"):

MPLINK 3.40, Linker
Copyright (c) 2003 Microchip Technology Inc.
Error - symbol 'bob' has multiple definitions.
Errors : 1


I'm positive i don't have multiple definitions. It appears that user_routines.h is being included several times. i understand repeated #defines would be protected by the


#ifndef __user_program_h_
#define __user_program_h_
....
#endif



but how do i protect real code (not #define's) from being repeatedly inserted?

The program compiles fine with the variables in user_routines.c, i'd just prefer these global constants to be in the header for convenience.

Gene F
17-05-2004, 15:05
I get the following error whenever i try to compile FRCcode with any variables declared in user_routines.h (in this case, "int bob;"):

MPLINK 3.40, Linker
Copyright (c) 2003 Microchip Technology Inc.
Error - symbol 'bob' has multiple definitions.
Errors : 1


I'm positive i don't have multiple definitions. It appears that user_routines.h is being included several times. i understand repeated #defines would be protected by the


#ifndef __user_program_h_
#define __user_program_h_
....
#endif



but how do i protect real code (not #define's) from being repeatedly inserted?

The program compiles fine with the variables in user_routines.c, i'd just prefer these global constants to be in the header for convenience.

The problem is that you are decalring bob in the header. It needs to be declared in the user_routines.c file and externed in the user_routines.h file.

So in user_routines.c it would look like:
int bob;
In user_routines.h it would be:
extern int bob;

What's happening is that each file that includes user_routines.h is creating a new copy of bob rather than just getting a reference to it.

Some compilers/linkers let you decare in the header file but in general it is not supported.

Max Lobovsky
17-05-2004, 15:41
Ah, right, i forgot about using extern when its in a different file. So the extern modifier tells the compiler to ignore multiple declarations?

Astronouth7303
18-05-2004, 07:36
No, extern tells the compiler that it is declared in another file, your just using it.

char foo = 0;
...
extern char foo;

wun
27-05-2004, 20:16
This sounds exactly like a problem I had.
It happened to me when I was trying to define variables in the .h files
You can either use extern, or just put them in the .c files (thats what I ended up doing)