tools.o(.data+0x0): multiple definition of `VAR_1'

S

Student

Hi all,
While compiling a program I had this message :

tools.o(.data+0x0): multiple definition of `VAR_1'
main.o(.data+0x0): first defined here
tools.o(.data+0x4): multiple definition of `VAR_2'
main.o(.data+0x4): first defined here
tools.o(.data+0x8): multiple definition of `VAR_3'
main.o(.data+0x8): first defined here
tools.o(.data+0xc): multiple definition of `VAR_4'
main.o(.data+0xc): first defined here
tools.o(.data+0x10): multiple definition of `VAR_5'
main.o(.data+0x10): first defined here

the structure is :
1/ main.c including a main.h where is defined VAR_1, ... ,VAR_5 as :
const char * VAR_1= ...
2/ In tools.c I include main.h too,
Yes it's normal with this situation that I got the message, but I use
in the head of main.h :
#ifndef MAIN_H
#define MAIN_H
....
#endif /* MAIN_H*/
which means (for me) that if it's already defined MAIN_H will not been
redefined.
To compile I use a makefile those are some lines of this makefile:

main.o: main.c
$(CC) -c $(ALL_CFLAGS) main.c -o main.o

tools.o:tools.c
$(CC) -c $(ALL_CFLAGS) tools.c -o tools.o

main: main.o tools.o
$(CC) main.o tools.o -o main $(LIBRARIES)

Anyone have an idea?

Best Regards.

Hicham
 
S

Student

I forgot to add that those variables are defined after declaration
using a simple #define
Thanks

Student a écrit :
 
J

Jack Klein

Hi all,
While compiling a program I had this message :

tools.o(.data+0x0): multiple definition of `VAR_1'
main.o(.data+0x0): first defined here
tools.o(.data+0x4): multiple definition of `VAR_2'
main.o(.data+0x4): first defined here
tools.o(.data+0x8): multiple definition of `VAR_3'
main.o(.data+0x8): first defined here
tools.o(.data+0xc): multiple definition of `VAR_4'
main.o(.data+0xc): first defined here
tools.o(.data+0x10): multiple definition of `VAR_5'
main.o(.data+0x10): first defined here

the structure is :
1/ main.c including a main.h where is defined VAR_1, ... ,VAR_5 as :
const char * VAR_1= ...
2/ In tools.c I include main.h too,
Yes it's normal with this situation that I got the message, but I use
in the head of main.h :
#ifndef MAIN_H
#define MAIN_H
...
#endif /* MAIN_H*/

You misunderstand the use of the preprocessor macros above. They
cause the contents of the included file to be ignored if it happens to
be included more than once in the same source file. They have no
effect if the include file is included in more than one source file.
which means (for me) that if it's already defined MAIN_H will not been
redefined.
To compile I use a makefile those are some lines of this makefile:

main.o: main.c
$(CC) -c $(ALL_CFLAGS) main.c -o main.o

tools.o:tools.c
$(CC) -c $(ALL_CFLAGS) tools.c -o tools.o

main: main.o tools.o
$(CC) main.o tools.o -o main $(LIBRARIES)

Anyone have an idea?

Best Regards.

Hicham

You are doing one very wrong thing, that is defining objects in a
header file.

Change the header file to read:

extern const char *VAR_1;
extern const char *VAR_2;

....and so on.

Then in one and only one of the source files, actually define them:

const char *VAR_1 = "whatever";

Make sure to include the header with the extern declarations in the
file that defines the objects, and in every file that refers to them.
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top