473,804 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global variable initialization using LIB project

Hi,

Actually the build of my solution gives as result an EXE and some DLLs.
Now, I am creating a new configuration that tranform the DLLs into LIBs.

The following semplified code explains one of the problems that arise
using a solution with a console project that creates the EXE and a lib
project that creates the LIB.

//------------------------------------------------------------------------
// console project

////Main Console file

#include "stdafx.h"
#include "registration.h "
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "# of registrations : [" << GetLen() << "]" << std::endl;

return 0;
}
//------------------------------------------------------------------------
// lib project

//// registration.h

#if !defined(REGIST RATION_H)
#define REGISTRATION_H

#include <set>

size_t GetLen();
bool Registration(in t);

#endif //REGISTRATION_H
//// registration.cp p

#include "stdafx.h"
#include "registration.h "

std::set<int> & GetTheSet()
{
static std::set<int> s_set;
return s_set;
}

size_t GetLen()
{
return GetTheSet().siz e();
}

bool Registration(in t id)
{
TRACE("registra tion of [%d]\n", id);
GetTheSet().ins ert(id);
return true;
}

namespace
{
const bool bReg1 = Registration(1) ;
} //!namespace
In this way all work fine and the output is

# of registrations : [1]

But the real case is this:

//------------------------------------------------------------------------
// lib project

////--- registration.h

#if !defined(REGIST RATION_H)
#define REGISTRATION_H

#include <set>

size_t GetLen();
bool Registration(in t);

#endif //REGISTRATION_H
////--- registration.cp p

#include "stdafx.h"
#include "registration.h "

std::set<int> & GetTheSet()
{
static std::set<int> s_set;
return s_set;
}

size_t GetLen()
{
return GetTheSet().siz e();
}

bool Registration(in t id)
{
TRACE("registra tion of [%d]\n", id);
GetTheSet().ins ert(id);
return true;
}

////--- source1.cpp

#include "stdafx.h"
#include "registration.h "

namespace
{
const bool bReg1 = Registration(1) ;
} //!namespace
In this way the output is

# of registrations : [0]

I am wondering where is the problem.
TIA.
Marco.

--
For direct reply change underscore to dot
Nov 17 '05 #1
0 1078

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

Similar topics

8
2536
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class static variables) is initializated (constructed) before main is invoked . . ." .. . .
4
1433
by: ypjofficial | last post by:
Pls look at this code ////////////////////start/////////// class a { public: a(){ cout<<"inside constructor of a"<<endl; } int b()
8
2561
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
33
3059
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
3
22818
by: Rahul Gandhi | last post by:
Hi, Which one preferable with respect to code size of the executable Un-initialised global variables or initialised global variables regards Rahul
12
2585
by: jyu.james | last post by:
I'm trying to detect reads of uninitialized global variables (that are declared in one file, and used in another as an extern). I know that ANSI C initializes all global variables to 0, however, I do not want to rely on this for initialization. Instead, I want to explicity initialize all variables myself. I've looked at tools like Compuware BoundsChecker, which does an amazing job in detecting uninitialized variables, but doesn't...
10
4215
by: n.torrey.pines | last post by:
Are global variables (and const's) guaranteed to be initialized before static class members (and methods) ? const int x = 19907; int get_x() { return x; } // another compilation unit: int get_x();
9
3038
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
29392
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
4
10913
by: koffee | last post by:
Hi people, I've got a question on global structure initialization. A global variable like int can be explicitly initialized as: int Global = 1; But how can I explicitly initialize a self-defined global structure? like:
0
10595
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
10343
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
10341
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
9171
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...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.