473,839 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(vo id)
{
static std::string book_title;
Book any_book;
book_title = any_book.get_ti tle();
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.l earn.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 8836
"Thomas Matthews" <Th************ *************@s bcglobal.net> wrote in
message news:41******** ******@sbcgloba l.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(vo id)
{
static std::string book_title;
Book any_book;
book_title = any_book.get_ti tle();
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************ *************@s bcglobal.net> wrote in
message news:41******** ******@sbcgloba l.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(vo id)
{
static std::string book_title;
Book any_book;
book_title = any_book.get_ti tle();
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.l earn.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
6550
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
7
1992
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(); somename.show() Note that the class name "somename" is reused for the variable name.
134
7941
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 means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
23
19214
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
1444
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; function foo() { alert(x);
4
2730
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 function, it is global regardless of whether it's declared with or without "var" * When it is declared inside a function, if declared with "var", it's local, if not, it's global
2
4722
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 declare the global variable...and it is having scope throughout the file then what is so different in extern variable???
2
3685
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
5505
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 that may print some messages. foo(...) { if (!silent)
0
9698
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10914
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10597
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10656
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10299
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7834
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.