A question about global variables

K

Ken Human

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
 
J

Jason

Ken said:
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
 
K

Ken Human

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top