Making a global variable

R

rosethorn

I'm modifying a pre-existing programme with a defined project
structure. I need to add a global variable that is visible in both an
implementation file in a library, and visible inside the main of a
programme that uses that library. This seems to be impossible, as any
approach I take either results in the variable being invisible in one
file, or the linker complaining about multiple definition. I'm using MS
VC++ 6.

The two basic approaches I've taken have been to either define the
variable in a header file, which results in multiple definition
complaints, or to try using extern declarations, which results in mere
invisibility.

Please help.
 
R

rosethorn

I should mention that I have tried including the variable in a
namespace, under both approaches, as well as not.
 
L

Larry Brasfield

I'm modifying a pre-existing programme with a defined project
structure. I need to add a global variable that is visible in both an
implementation file in a library, and visible inside the main of a
programme that uses that library. This seems to be impossible, as any
approach I take either results in the variable being invisible in one
file, or the linker complaining about multiple definition. I'm using MS
VC++ 6.

The two basic approaches I've taken have been to either define the
variable in a header file, which results in multiple definition
complaints, or to try using extern declarations, which results in mere
invisibility.


If the library is statically linked, your latter approach
should have worked and its failure to do so will have
to be diagnosed with the benefit of seeing some code.

If the library is dynamically linked, your problem is
off-topic here and should be taken to one of the
groups in the microsoft.public.* hierarchy.
 
H

Howard

I'm modifying a pre-existing programme with a defined project
structure. I need to add a global variable that is visible in both an
implementation file in a library, and visible inside the main of a
programme that uses that library. This seems to be impossible, as any
approach I take either results in the variable being invisible in one
file, or the linker complaining about multiple definition. I'm using MS
VC++ 6.

The two basic approaches I've taken have been to either define the
variable in a header file, which results in multiple definition
complaints, or to try using extern declarations, which results in mere
invisibility.

Please help.

I'm a bit confused about exacytly what you have tried, since you don't show
any code. But in general, assuming I was forced to use a global variable in
the first place (yuk), I'd declare the variable as extern in a common header
in my library (which implementation files in both the library and the
program will include), and define it in the implementation file in the
library.

(I suspect this is in the FAQ, but I'm too lazy to look myself right now.
:))

-Howard
 
R

rosethorn

Larry said:
If the library is statically linked, your latter approach
should have worked and its failure to do so will have
to be diagnosed with the benefit of seeing some code.

Indeed, it is statically linked. In a development that increasingly
makes me hate computers in all ways, this seems to have suddenly
started to work, after a mere 3 hours of doing substantially the same
thing, interleaved with reading confirming my approach.

Thanks for your help, and for responding so soon.
 
J

Jaap Versteegh

header file :

#ifndef GLOGAL
#define GLOBAL

int Globalvariable;

#endif

This won't help when the header is included in multiple source files, will it ?

Multiple definition, reported by the linker this time...

Cheers, Jaap
 
M

Michael Etscheid

Jaap said:
This won't help when the header is included in multiple source files,
will it ?

Multiple definition, reported by the linker this time...

Cheers, Jaap

Just do it like Howard said:

"I'd declare the variable as extern in a common header
in my library (which implementation files in both the library and the
program will include), and define it in the implementation file in the
library."

In code:

// testing.h
#ifndef TESTING_H
#define TESTING_H

extern int i;

void foo();

#endif

// testing.cpp
#include "testing.h"

int i;

void foo()
{
i = 6;
}

// main.cpp
#include "testing.h"
#include <iostream>
using namespace std;

int main()
{
foo();
cout << i;
}
 
D

DHOLLINGSWORTH2

Dont confuse the header file, for a cpp file.
The header is only there to allow multiple file projects to have a common
interface before linking.

You should give info about the global variable in header:

extern int Globalinfo;

while keeping the Actual code in the cpp :

int Globalinfo;

And make sure you 1. include the header, and 2. link the code.

Link the code once, and once only, and there is only one copy of the data,
howver you can include headers all day long, this tells the other code that
there will be a copy of the data at link time.

I see a lot of people putting code in header files, while that may work in
some cases, it is a bad habit to get into, and will cause problems sooner or
later.
 
M

Michael Etscheid

DHOLLINGSWORTH2 said:
Dont confuse the header file, for a cpp file.
The header is only there to allow multiple file projects to have a common
interface before linking
You should give info about the global variable in header:

extern int Globalinfo;

while keeping the Actual code in the cpp :

int Globalinfo;

And make sure you 1. include the header, and 2. link the code.

Link the code once, and once only, and there is only one copy of the data,
howver you can include headers all day long, this tells the other code that
there will be a copy of the data at link time.

I see a lot of people putting code in header files, while that may work in
some cases, it is a bad habit to get into, and will cause problems sooner or
later.

Eh, I'm practising your tips or do I missunderstand you?
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top