acces to global variabels from more than one class

  • Thread starter Jan-Henrik Grobe
  • Start date
J

Jan-Henrik Grobe

Hallo,

normally I am not the one who calls a Newsgroup if something does not work.
But now I am frustrated.
I did write a program with a lot classes. I use some gloabl variables which
are in a file called global.h. Now two of my classes access this variables.
One classes changes the values (writes) and another read them out.

So far so good...under Visual Studio.NET I declared these globals with
__declspec(selectany) and it worked. Now I with to convert everything to
Linux (Fedora Core 2.0), but the g++ compiler does not accept these
__declspec(selectany). I tried it with "extern" but it didnt work, too. I
always get errors like "multiple definitions of xxxxxx"

My question now is, if saomebody knows about such a problems and can provide
a way to solve it.

Many Thanks
Jan-Henrik
 
N

Nicolas Pavlidis

Jan-Henrik Grobe said:
Hallo,

normally I am not the one who calls a Newsgroup if something does not work.
But now I am frustrated.
I did write a program with a lot classes. I use some gloabl variables which
are in a file called global.h. Now two of my classes access this variables.
One classes changes the values (writes) and another read them out.

So far so good...under Visual Studio.NET I declared these globals with
__declspec(selectany) and it worked. Now I with to convert everything to
Linux (Fedora Core 2.0), but the g++ compiler does not accept these
__declspec(selectany). I tried it with "extern" but it didnt work, too. I
always get errors like "multiple definitions of xxxxxx"

__declspec is not Standrad C++, so g++ rejects it. After reading the
description of __declspec(selectany) I think, that extern and this
"keyword" is not the same thing. Do you include the global.h - file or
to you declare the two gloabls in each file where you need them?

Please post the part of the code wich makes the problems.
Maybe you forgot to provied include-guards, in case you are including
global.h, but thats only guessing without a piece of code.

Kind regards,
Nicolas
 
J

Jonathan Bartlett

So far so good...under Visual Studio.NET I declared these globals with
__declspec(selectany) and it worked. Now I with to convert everything to
Linux (Fedora Core 2.0), but the g++ compiler does not accept these
__declspec(selectany). I tried it with "extern" but it didnt work, too. I
always get errors like "multiple definitions of xxxxxx"

Be sure that the variable is only _defined_ once.

For example, if I have

extern int i;

in my header file, then I should have ONLY AND EXACTLY ONE

int i;

in any of my C source files. As in, pick ONE file to put the "int i;"
in, and DONT PUT IT ANYWHERE ELSE.

"extern int i;" tells the compiler that somewhere there will be a
variable called "i" that is global.

"int i;" tells the compiler "it's right here". If you say "int i;"
multiple places, the compiler will get confused.

Jon
 
J

Jan-Henrik Grobe

Nicolas Pavlidis said:
__declspec is not Standrad C++, so g++ rejects it. After reading the
description of __declspec(selectany) I think, that extern and this
"keyword" is not the same thing. Do you include the global.h - file or to
you declare the two gloabls in each file where you need them?

Please post the part of the code wich makes the problems.
Maybe you forgot to provied include-guards, in case you are including
global.h, but thats only guessing without a piece of code.

Kind regards,
Nicolas

Hallo,

many thanks for the help attempt...my code is a little longer but I found a
good example that shows the problem. In this example the functions
computeTime and output Time are in two different files. The third file is
global.h

global.h:

__declspec(selectany) int globalhour = 0;
__declspec(selectany) int globalminute = 0;
__declspec(selectany) int globalsecond = 0;



void computeTime(void)
{
// imagine a function that gets the actual hour, minute and second

globalhour = hour;
globalminute = minute;
globalsecond = second;
}


void outputTime(void)
{
printf("Now it is exactly %d hours, %d minutes and %d seconds \n",
globalhour,

globalminute,

globalsecond);
}

if I compile my code with these __declspecs I got this error:

"ISO C++ forbits declaration of "__declspec" with no type"

So I am assuming the compiler doesnt know this construction. I am looking
for an alternative for Linux.

Many thanks
Jan-Henrik
 
N

Nicolas Pavlidis

Jan-Henrik Grobe said:
Hallo,

many thanks for the help attempt...my code is a little longer but I found a
good example that shows the problem. In this example the functions
computeTime and output Time are in two different files. The third file is
global.h

global.h:

[...]


if I compile my code with these __declspecs I got this error:

"ISO C++ forbits declaration of "__declspec" with no type"

Yes, because __declspec is a microsoft specific thing, and not Stdandard
C++.
So I am assuming the compiler doesnt know this construction. I am looking
for an alternative for Linux.

Did you try the follwoing?
#ifndef global_h___
#define global_h___

// define here the gloabal variables.
#endif

That is called include - guard.
 
J

Jonathan Mcdougall

Jan-Henrik Grobe said:
Hallo,

normally I am not the one who calls a Newsgroup if something does not
work. But now I am frustrated.
I did write a program with a lot classes. I use some gloabl variables
which are in a file called global.h. Now two of my classes access this
variables. One classes changes the values (writes) and another read them
out.

So far so good...under Visual Studio.NET I declared these globals with
__declspec(selectany) and it worked. Now I with to convert everything to
Linux (Fedora Core 2.0), but the g++ compiler does not accept these
__declspec(selectany). I tried it with "extern" but it didnt work, too. I
always get errors like "multiple definitions of xxxxxx"

With no code, it is quite difficult and I can only guess. You should put the
definition of global variables in one place only (therefore, NOT in
headers).

// globals.cpp

int my_global=0;

You then only have to declare these globals in a header.

// globals.h

extern int my_global;

To use it, include globals.h which does not define the variable, it only
declares it. globals.cpp defines the global variables only once, so both
the compiler and linker are happy.



Jonathan
 
J

Jack Klein

Jan-Henrik Grobe said:
Hallo,

many thanks for the help attempt...my code is a little longer but I found a
good example that shows the problem. In this example the functions
computeTime and output Time are in two different files. The third file is
global.h

global.h:

[...]


if I compile my code with these __declspecs I got this error:

"ISO C++ forbits declaration of "__declspec" with no type"

Yes, because __declspec is a microsoft specific thing, and not Stdandard
C++.
So I am assuming the compiler doesnt know this construction. I am looking
for an alternative for Linux.

Did you try the follwoing?
#ifndef global_h___
#define global_h___

No, don't do this. This violates the C++ standard. Any identifier in
any context with two consecutive underscores is reserved for the
implementation.
 
N

Nicolas Pavlidis

Jack said:
Jan-Henrik Grobe wrote:

Hallo,

many thanks for the help attempt...my code is a little longer but I found a
good example that shows the problem. In this example the functions
computeTime and output Time are in two different files. The third file is
global.h

global.h:

[...]


if I compile my code with these __declspecs I got this error:

"ISO C++ forbits declaration of "__declspec" with no type"

Yes, because __declspec is a microsoft specific thing, and not Stdandard
C++.

So I am assuming the compiler doesnt know this construction. I am looking
for an alternative for Linux.

Did you try the follwoing?
#ifndef global_h___
#define global_h___


No, don't do this. This violates the C++ standard. Any identifier in
any context with two consecutive underscores is reserved for the
implementation.
Sorry *gettingred*
Thank you for the correction, here an includegurad that confirms to the
standard:
#ifndef GLOBAL_H_
#define GLOBAL_H_

#endif

Best regards,
Nicolas
 
J

Jan-Henrik Grobe

Hallo,

I just wish to say thank you to everyone who helped me. Both varieties
worked for me. The include guard method and the declaration in the header
and initializing in the .cpp.

Many thanks
Jan
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top