470,571 Members | 2,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

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

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

Aug 21 '06 #1
3 5069
I forgot to add that those variables are defined after declaration
using a simple #define
Thanks

Student a écrit :
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
Aug 21 '06 #2
On 21 Aug 2006 02:52:34 -0700, "Student" <gh***********@gmail.com>
wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 22 '06 #3
Thank you Jack, I have not pay attention to this.

Jack Klein a écrit :
On 21 Aug 2006 02:52:34 -0700, "Student" <gh***********@gmail.com>
wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jochen Zeischka | last post: by
11 posts views Thread by Georg Teichtmeister | last post: by
2 posts views Thread by Christian Christmann | last post: by
10 posts views Thread by zfareed | last post: by
3 posts views Thread by Antonio Rivas | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.