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

A question about global variables

P: n/a
I am writing a program that contains multiple source and header files,
generally one of each for classes that are similar to each other. I
want to be able to declare an object of the class declared in a specific
header file, and include that header in the source file of a different
set of classes so that those classes will have access to that object. I
believe I'm going about this in the wrong way, any advice would be
appreciated. A demonstration follows:

/*main.cpp*/

#include "a.h"
#include "b.h"

int main() {
B myB;
return 0;
}

/*a.h*/

#ifndef A_H
#define A_H

class A {
public:
A();
};

A myA;

#endif

/*b.h*/

#ifndef B_H
#define B_H

class B {
public:
B();
};

#endif

/*a.cpp*/

#include "a.h"

A::A() { }

/*b.cpp*/

#include "b.h"
#include "a.h"

B::B() { A newA = A(myA); }

I get link errors when building this project:

..NET 2005:

a.obj : error LNK2005: "class A myA" (?myA@@3VA@@A) already defined in
main.obj
b.obj : error LNK2005: "class A myA" (?myA@@3VA@@A) already defined in
main.obj

Cygwin:

ken@ken-wn0vf73qmks ~/c
$ g++ main.cpp a.cpp b.cpp -o main -Wall
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccZtqXQj.o(.bss+0x0):a.cpp:
multiple definition of `_myA'
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccSiR2mi.o(.bss+0x0):main.cpp:
first defined here
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccKtlZoq.o(.bss+0x0):b.cpp:
multiple definition of `_myA'
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccSiR2mi.o(.bss+0x0):main.cpp:
first defined here
collect2: ld returned 1 exit status
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Ken Human wrote:
I am writing a program that contains multiple source and header files, generally one of each for classes that are similar to each other. I
want to be able to declare an object of the class declared in a specific header file, and include that header in the source file of a different set of classes so that those classes will have access to that object. I believe I'm going about this in the wrong way, any advice would be
appreciated. A demonstration follows:

/*main.cpp*/

#include "a.h"
#include "b.h"

int main() {
B myB;
return 0;
}

/*a.h*/

#ifndef A_H
#define A_H

class A {
public:
A();
};

A myA;
Ack! You almost NEVER want to do this. Every compilation unit that
includes this header will not create this variable, and that is the
error that you are getting.

Instead use extern A myA;

#endif

/*b.h*/

#ifndef B_H
#define B_H

class B {
public:
B();
};

#endif

/*a.cpp*/

#include "a.h"

A::A() { }
Now HERE you can declare your variable without problems.

A myA;


/*b.cpp*/

#include "b.h"
#include "a.h"

B::B() { A newA = A(myA); }

I get link errors when building this project:

.NET 2005:

a.obj : error LNK2005: "class A myA" (?myA@@3VA@@A) already defined in main.obj
b.obj : error LNK2005: "class A myA" (?myA@@3VA@@A) already defined in main.obj

Cygwin:

ken@ken-wn0vf73qmks ~/c
$ g++ main.cpp a.cpp b.cpp -o main -Wall
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccZtqXQj.o(.bss+0x0):a.cpp:
multiple definition of `_myA'
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccSiR2mi.o(.bss+0x0):main.cpp: first defined here
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccKtlZoq.o(.bss+0x0):b.cpp:
multiple definition of `_myA'
/cygdrive/c/DOCUME~1/ken/LOCALS~1/Temp/ccSiR2mi.o(.bss+0x0):main.cpp: first defined here
collect2: ld returned 1 exit status


Do these error make more sense now? Before, you included a.h in a.cpp,
main.cpp and b.cpp. As a result you declared A myA (indirectly via a.h)
THREE TIMES. The linker first registered this variable in main.o. When
it saw it AGAIN in a.cpp and AGAIN in b.cpp, it freaked out.

Don't feel embarassed, this is a very common mistake. I'd bet that
we've all done this before when we were learning the basics.

-Jason

Jul 23 '05 #2

P: n/a
Jason wrote:
[...]


Do these error make more sense now? Before, you included a.h in a.cpp,
main.cpp and b.cpp. As a result you declared A myA (indirectly via a.h)
THREE TIMES. The linker first registered this variable in main.o. When
it saw it AGAIN in a.cpp and AGAIN in b.cpp, it freaked out.

Don't feel embarassed, this is a very common mistake. I'd bet that
we've all done this before when we were learning the basics.

-Jason


Thanks for your explanation, Jason. I had tried using "extern A myA" in
my header file before but I was not aware that I had to also define it
in my source file. So my understanding of the extern keyword was lacking.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.