Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple definition of error.

Status
Not open for further replies.

kama1

Programmer
Joined
Aug 5, 2001
Messages
5
Location
SE
Hi,

I get multiple defs even if i do a ifndef around it. I need to get a couple of global variables in different files.

these are my test files:

//a.h

#ifndef _A_H_
#define _A_H_
int a;
void add();
#endif

//a.cpp
#include "a.h"

add() { a++; }

//main.cpp
#include "a.h"

void main() {
a=3;
add();
cout << a << endl;
}

bash-2.03$ g++ a.o main.cpp
/tmp/cccRvDFI.o(.data+0x0): multiple definition of `a'
a.o(.data+0x0): first defined here
bash-2.03$

/bjorn
 
#ifndef _A_H_
#define _A_H_
extern int a;
extern void add();
#endif John Fill
1c.bmp


ivfmd@mail.md
 
You have included the header file a.h in both (a.cpp & main.cpp) the cpp files. While building a project you will get this error because you have defined the variable 'a' in a.cpp and in main.cpp.

Declare the variable as extern int a; in the a.h and define it as 'int a' in a.cpp or in main.cpp.

Regards,
Maniraja S
 
just use keyword extern in h files. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top