Connecting Tech Pros Worldwide Forums | Help | Site Map

Making a global variable

rosethorn@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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.


rosethorn@gmail.com
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Making a global variable


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

Larry Brasfield
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Making a global variable


<rosethorn@gmail.com> wrote in message
news:1110565254.573315.87990@o13g2000cwo.googlegro ups.com...[color=blue]
> 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.[/color]


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.

--
--Larry Brasfield
email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me.


Howard
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Making a global variable



<rosethorn@gmail.com> wrote in message
news:1110565254.573315.87990@o13g2000cwo.googlegro ups.com...[color=blue]
> 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.
>[/color]

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


rosethorn@gmail.com
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Making a global variable



Larry Brasfield wrote:
[color=blue]
> 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.[/color]

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.

DHOLLINGSWORTH2
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Making a global variable


header file :

#ifndef GLOGAL
#define GLOBAL

int Globalvariable;

#endif


Jaap Versteegh
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Making a global variable


[color=blue]
> header file :
>
> #ifndef GLOGAL
> #define GLOBAL
>
> int Globalvariable;
>
> #endif[/color]

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
Michael Etscheid
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Making a global variable


Jaap Versteegh schrieb:[color=blue]
>[color=green]
>> header file :
>>
>> #ifndef GLOGAL
>> #define GLOBAL
>>
>> int Globalvariable;
>>
>> #endif[/color]
>
>
> 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[/color]

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;
}
DHOLLINGSWORTH2
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Making a global variable


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.


Michael Etscheid
Guest
 
Posts: n/a
#10: Jul 23 '05

re: Making a global variable


DHOLLINGSWORTH2 schrieb:[color=blue]
> 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.[/color]

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