By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,318 Members | 2,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,318 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.