Re: Variable declaration: Global vs. Function

From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 01/26/05


Date: Wed, 26 Jan 2005 19:45:04 GMT

Mike Wahler wrote:
> "Thomas Matthews" <Thomas_MatthewsSpamBotsSuck@sbcglobal.net> wrote in
> message news:41F7B848.5020906@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


Relevant Pages

  • Re: how to declare global Class library project?
    ... Easist way would be a web service. ... In .NET libraries are loaded into their calling process. ... supporting remoting (i.e a winforms app that has a singleton instance of the ... > referencing this global declaration from each of the 2 ...
    (microsoft.public.dotnet.languages.vb)
  • Re: The Ulimate Editor
    ... the parameters to CALL have always been in the reverse order to a standard C call. ... W32F does not use the count of parameters to manipulate the stack before the call; ... a function as a normal Forth word, without too much declaration fluff; preferably just the name of the library it lives in, as I can do right now in Win32Forth. ... of external libraries ...
    (comp.lang.forth)
  • Re: compiling taucs problem (Re: solving Ax=b with sparse A)
    ... The function declaration in line 6 of spmat.cc ... exception are fortran libraries that you call from C, ... int main{ ... I can't see what I am doing wrong while compiling the example ...
    (sci.math.num-analysis)
  • Re: please help
    ... This is the library declaration: ... libraries built on Cygwin but not MinGW. ...
    (comp.lang.fortran)
  • Re: A puzzling error re. how object is defined
    ... >> The problem has to do with the order in which the global objects merr ... declaration of global variables has anything to do with the order of ... different compiles (on the same compiler) for unpredictable reasons. ... should never rely on the order of construction of globally defined ...
    (comp.unix.programmer)