473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Variable declaration: Global vs. Function

Hi,

I'm getting linking errors when I declare a variable in the
global scope, but not inside a function. The declarations
are the same (only the names have been changed...).

class Book
{
public:
Book()
{ ; }
virtual ~Book() { ; }
string get_title(void) const
{ return title;}
private:
std::string title;
std::string author;
};

/* Global scope declaration */
Book Global_Book;

/* Inside function declaration */
void Any_Function(void)
{
static std::string book_title;
Book any_book;
book_title = any_book.get_title();
return;
}

I am throwing this code into a library, and the library
reports:
U global destructors keyed to Global_Book
U global constructors keyed to Global_Book
If I comment-out the global declaration, these messages
disappear {of course}.

So, why are there constructors & destructors for a global
instance, but not for an instance local to the function?

If I change the declaration of "any_book" in the function
to static, there are still no "global constructors" listed
for it (I would think there would be).

I am using g++ (GCC) 3.3.1 (cygming special).

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #1
2 8785
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:41**************@sbcglobal.net...
Hi,

I'm getting linking errors when I declare a variable in the
global scope, but not inside a function. The declarations
are the same (only the names have been changed...).

class Book
{
public:
Book()
{ ; }
virtual ~Book() { ; }
Note that those empty statements in your
ctor and dtor are not needed.
string get_title(void) const
{ return title;}
private:
std::string title;
std::string author;
};

/* Global scope declaration */
Book Global_Book;

/* Inside function declaration */
void Any_Function(void)
{
static std::string book_title;
Book any_book;
book_title = any_book.get_title();
return;
}

I am throwing this code into a library,
So now we can't know the full context, which imo
is very likely relevant.

and the library
reports:
U global destructors keyed to Global_Book
U global constructors keyed to Global_Book
The 'library' reports? Or a compiler, or a linker?
I don't understand.
If I comment-out the global declaration, these messages
disappear {of course}.
I don't know what those messages about 'keyed' mean.
What does your documentation say?

So, why are there constructors & destructors for a global
instance, but not for an instance local to the function?
Must be some propery of the library, or your library manager.
A wild guess: Since I suspect you've only showed a 'skeleton'
example, I'll ask: do any of your global objects depend upon
other global objects for construction, in a particular order
(the language does not specify this order for global objects
between translation units.

If I change the declaration of "any_book" in the function
to static, there are still no "global constructors" listed
for it (I would think there would be).
'static' does not mean 'global'. Inside a function, it
indicates a storage duration, nothing to do with scope.

I am using g++ (GCC) 3.3.1 (cygming special).


I think you might have a platform-specific or tool-specific
issue, but I can't say conclusively without more context.
Have you tried any methodical, incremental build-and-testing?

-Mike
Jul 23 '05 #2
Mike Wahler wrote:
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:41**************@sbcglobal.net...
Hi,

I'm getting linking errors when I declare a variable in the
global scope, but not inside a function. The declarations
are the same (only the names have been changed...).

class Book
{
public:
Book()
{ ; }
virtual ~Book() { ; }

Note that those empty statements in your
ctor and dtor are not needed.


Noted, but I do that for sytlistic purposes.
So, what's a little wasted compiler time, in
the big picture? {Rhetorical}


string get_title(void) const
{ return title;}
private:
std::string title;
std::string author;
};

/* Global scope declaration */
Book Global_Book;

/* Inside function declaration */
void Any_Function(void)
{
static std::string book_title;
Book any_book;
book_title = any_book.get_title();
return;
}

I am throwing this code into a library,

So now we can't know the full context, which imo
is very likely relevant.


In other words, there is a third party involved:
source -> compiler -> librarian -> linker.
Unix libraries and builds have always been a
thorn for me.
and the library
reports:
U global destructors keyed to Global_Book
U global constructors keyed to Global_Book

The 'library' reports? Or a compiler, or a linker?
I don't understand.

Actually, this is a report generated from the library.
The 'U' indicates undefined symbol in the library.
In Cygwin / unix terms:
nm --demangle my_library.a

If I comment-out the global declaration, these messages
disappear {of course}.

I don't know what those messages about 'keyed' mean.
What does your documentation say?

Yep, that's what I'm having an issue with.
I was wondering if the C++ language specification had
any information about construction of global objects
and how it differs from construction of function-local
or static variables in functions.

So, why are there constructors & destructors for a global
instance, but not for an instance local to the function?

Must be some propery of the library, or your library manager.
A wild guess: Since I suspect you've only showed a 'skeleton'
example, I'll ask: do any of your global objects depend upon
other global objects for construction, in a particular order
(the language does not specify this order for global objects
between translation units.


My understanding is that constructors are called for all
objects. Just the order of construction and residence of
the object are the difference between global, function-local
and function-static variables.
If I change the declaration of "any_book" in the function
to static, there are still no "global constructors" listed
for it (I would think there would be).

'static' does not mean 'global'. Inside a function, it
indicates a storage duration, nothing to do with scope.


Understood. A variable declared as static within a function
has the same life time as a global variable. Many compilers
place these two kinds of variables in the same area. My
understanding is that the global variable is constructed
sometime before "main" and the function-static variable is
construction upon the first entry to the function (the basis
of the Singleton pattern}.

I am using g++ (GCC) 3.3.1 (cygming special).

I think you might have a platform-specific or tool-specific
issue, but I can't say conclusively without more context.
Have you tried any methodical, incremental build-and-testing?

-Mike


The issue came about because I had a static constant array
of Books. I narrowed down to just one global instance
(removing the module static declaration). This gives the
error. However, sticking the variable inside a function
does not generate the error. This is the basis for
my issue. I have instances of this variable in many
functions. Its only the global declaration that is the
problem.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
13
by: Joe Attardi | last post by:
Over the weekend I attended a session on JavaScript at the No Fluff Just Stuff conference and learned an interesting quirk that I wanted to ask a question about.. Take this code: x = 5; ...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.