This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: link error: multiple definition of


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]