public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* link error: multiple definition of
@ 2002-10-17  7:02 zhjin
  2002-10-17 13:49 ` Mike Stump
  0 siblings, 1 reply; 2+ messages in thread
From: zhjin @ 2002-10-17  7:02 UTC (permalink / raw)
  To: gcc

hi
  this is my code under redhat linux 8.0.

//a.h
#ifndef ah
#define ah
int i;
void add();
#endif 

//a.cpp
#include "a.h"
void add(){
  i++;
}

//c.cpp
#include "a.h"
int main(){
  i = 100;
  add();
  return 0;
}

g++ -c a.cpp     success
g++ c.cpp a.o 
error: a.o(.bss+0x0): multiple definition of 'i'
/tmp/cc0q7tmK.o(.bss+0x0): first defined here
collect2: ld returned 1 exit status.

can someone help me to solve problem? 
thanks.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: link error: multiple definition of
  2002-10-17  7:02 link error: multiple definition of zhjin
@ 2002-10-17 13:49 ` Mike Stump
  0 siblings, 0 replies; 2+ messages in thread
From: Mike Stump @ 2002-10-17 13:49 UTC (permalink / raw)
  To: zhjin; +Cc: gcc

On Wednesday, October 16, 2002, at 10:05 PM, zhjin wrote:
> this is my code under redhat linux 8.0.
>
> //a.h
> #ifndef ah
> #define ah
> int i;
> void add();
> #endif
>
> //a.cpp
> #include "a.h"
> void add(){
>   i++;
> }
>
> //c.cpp
> #include "a.h"
> int main(){
>   i = 100;
>   add();
>   return 0;
> }
>
> g++ -c a.cpp     success
> g++ c.cpp a.o
> error: a.o(.bss+0x0): multiple definition of 'i'
> /tmp/cc0q7tmK.o(.bss+0x0): first defined here
> collect2: ld returned 1 exit status.
>
> can someone help me to solve problem?

This is the wrong list for help in programming in C++.  I'd recommend a 
book on C++.  In C++ you are allowed to define some things once.  
Definitions for these things go in .c files.  You can declare them as 
many times as you want.

extern int i;

is a declaration that i will exist, and it is an int.

int i;

is a definition of i.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-10-17 16:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-17  7:02 link error: multiple definition of zhjin
2002-10-17 13:49 ` Mike Stump

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).