accessing variables from more than one .cpp file

S

scott

hi, sorry if this has been adressed some where elss. if it has plz can you
point me to it and ill go read it.

any way, i want to declare some variables with in a header file. lets say
int i;

i then want to be able to acess that variable from multipul .cpp files where
I can read it and also change the value. i would like to hold it in a
struct or a class eventualy but i can't even get it to work on its own at
min, so i thought id start off simple and ask how i do it just like what i
described above.

any help would be grate,

thx scott
 
J

John Harrison

scott said:
hi, sorry if this has been adressed some where elss. if it has plz can you
point me to it and ill go read it.

any way, i want to declare some variables with in a header file. lets say
int i;

i then want to be able to acess that variable from multipul .cpp files
where
I can read it and also change the value. i would like to hold it in a
struct or a class eventualy but i can't even get it to work on its own at
min, so i thought id start off simple and ask how i do it just like what i
described above.

any help would be grate,

thx scott

Put this in the header file

extern int i;

Put this in *one* .cpp file

int i;

Include the header file in every .cpp file where you need to use it.

What goes in the header file is called a declaration, what goes in the one
..cpp file is called a definition. You can have as many declarations as you
like but you must have only have one definition. This is the One Definition
Rule. Understanding the difference between declarations and definitions is
an important C++ concept to master.

BTW i is a fantastically bad name for a global variable.

john
 
V

Victor Bazarov

scott said:
hi, sorry if this has been adressed some where elss. if it has plz can you
point me to it and ill go read it.

any way, i want to declare some variables with in a header file. lets say
int i;

If you want to declare it, add 'extern' to it:

extern int i;
i then want to be able to acess that variable from multipul .cpp files where
I can read it and also change the value. i would like to hold it in a
struct or a class eventualy but i can't even get it to work on its own at
min, so i thought id start off simple and ask how i do it just like what i
described above.

Include that header wherever you need that variable. Then in only one
of the files add a definition of that variable:

int i;

(to make it a true definition you could also initialise it to something:

int i = 42;

}

Then compile and link. All should work.

Oh, and find yourself a good book. Things like this gotta be covered in
them. I can't imagine that they wouldn't be.

Victor
 
D

David Lindauer

an alternative is to declare the variables like this:

#ifndef EXTERN
#define EXTERN extern
#endif

EXTERN int i;

Then include the file wherever you want, but in exactly one place (probably
wherever MAIN or WinMain is)
do this before including it:

#define EXTERN

That will default the declarations to 'extern' linkage but allow you to override
it in the one place the things need to be made globals.

David
 
J

JKop

scott posted:
hi, sorry if this has been adressed some where elss. if it has plz can
you point me to it and ill go read it.

any way, i want to declare some variables with in a header file. lets
say int i;

i then want to be able to acess that variable from multipul .cpp files
where I can read it and also change the value. i would like to hold it
in a struct or a class eventualy but i can't even get it to work on its
own at min, so i thought id start off simple and ask how i do it just
like what i described above.

any help would be grate,

thx scott


A C++ program is a collection of ".cpp" files and nothing more.

When you compile a C++ program, you supply the compiler with the ".cpp"
files, for example:

g++ monkey.cpp cow.cpp orange.cpp

The compiler will create an executable for you.

The "monkey.cpp" can use functions and global variables defined in the two
other files, "cow.cpp" and "orange.cpp", and vice versa.


So let's say in "cow.cpp", you have a global variable as so:

//cow.cpp

int cow = 7;


Now, while "monkey.cpp" is being compiled, the compiler comes across:

//monkey.cpp

int main()
{
cow += 6;
}


But... the problem here is that "monkey.cpp" hasn't got a clue about the
variable "cow". To remedy the situation, if you were to do:

//monkey.cpp

int cow;

int main()
{
cow += 6;
}


Then there would be no problem at all with "monkey.cpp", but then when the
compiler goes to compile the three files together it will say:

ERROR: Multiple Definitions

So... what you want is a declaration, which says: "Don't actually create
(define) a variable, all I want to do is let you know about one that already
exists". You do this as follows:

//monkey.cpp

extern int cow;

int main()
{
cow +=4;
}


//cow.cpp

int cow;


Now when you compile "monkey.cpp" and "cow.cpp" together, everything will be
perfect.

But...

if you compile "monkey.cpp" on its own, there will be an error - you access
the variable "cow", so there must be a definition of it.

So... in summation...

If you want to have a global variable and give other .cpp files access to it
via a header file, this is how it's done:


//blah.hpp

extern int blah;


//blah.cpp

int blah = 5;



Then all the other .cpp files have to do is include "blah.hpp". And one more
thing... when they're compiling their program, they've to put in "blah.cpp"
with it aswell, for example:

g++ myfile.cpp blah.cpp


One more thing:


When you put the following in a ".cpp" file:

#include "blah.hpp"


What happens is that the exact contents of the header file get copy-and-
pasted into where the #include line was.


And one more little thing: Google for "inclusion guards".


-JKop
 
S

scott urban

an alternative is to declare the variables like this:

#ifndef EXTERN
#define EXTERN extern
#endif

EXTERN int i;

Then include the file wherever you want, but in exactly one place
(probably wherever MAIN or WinMain is) do this before including it:

#define EXTERN

That will default the declarations to 'extern' linkage but allow you
to override it in the one place the things need to be made globals.

David

This seems really clumsy to me. You've replaced the need to define the
variable in exaclty one translation unit with the need to define a
preprocessor symbol in exactly one translation unit. Putting the two
approaches side-by-side shows this:

-------------------
Your solution

// file foo.h
#ifndef EXTERN
#define EXTERN extern
#endif
EXTERN int i;

// file foo_user1.cpp
#include "foo.h"

// file foo_user2.cpp
#define EXTERN
#include "foo.h"

-------------------
Typical solution

// file foo.h
extern int i;

// file foo_user1.cpp
#include "foo.h"

// file foo_user2.cpp
#include "foo.h"
int i = 0;


So it's the same conceptually, yet less clear to other people and more
lines of code, and also probably doesn't scale well to the purpose
of defining multiple globals in different translaction units.
 
D

David Lindauer

scott said:
This seems really clumsy to me. You've replaced the need to define the
variable in exaclty one translation unit with the need to define a
preprocessor symbol in exactly one translation unit. Putting the two
approaches side-by-side shows this:

I use that approach in some projects, because where I work it is common to
have a single global header file that defines *everything*. Rather than
maintain two versions of each variable declaration (which both have to be
created and then modified if there are changes) I do this. I agree if you
are scattering your globals
all over in different files this approach isn't very clear, however. But
for the case where you have a single global file it actually works out a
little better... and in reality it doesn't matter *where* you define things
as long as they get defined.

David
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top