Variable declaration: Global vs. Function

T

Thomas Matthews

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
 
M

Mike Wahler

Thomas Matthews said:
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
 
T

Thomas Matthews

Mike said:
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}

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.


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

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.

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.


'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 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
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top