471,089 Members | 1,068 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

A question about global variables

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
2 3412

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
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.

Similar topics

8 posts views Thread by lian | last post: by
10 posts views Thread by Matt | last post: by
2 posts views Thread by Bryan Parkoff | last post: by
11 posts views Thread by Capstar | last post: by
3 posts views Thread by Ara Kooser | last post: by
17 posts views Thread by kumar | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.