473,480 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Create 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(REGISTRATION_H)
#define REGISTRATION_H

#include <set>

size_t GetLen();
bool Registration(int);

#endif //REGISTRATION_H
//// registration.cpp

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

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

size_t GetLen()
{
return GetTheSet().size();
}

bool Registration(int id)
{
TRACE("registration of [%d]\n", id);
GetTheSet().insert(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(REGISTRATION_H)
#define REGISTRATION_H

#include <set>

size_t GetLen();
bool Registration(int);

#endif //REGISTRATION_H
////--- registration.cpp

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

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

size_t GetLen()
{
return GetTheSet().size();
}

bool Registration(int id)
{
TRACE("registration of [%d]\n", id);
GetTheSet().insert(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 1068

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

Similar topics

8
2513
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...
4
1417
by: ypjofficial | last post by:
Pls look at this code ////////////////////start/////////// class a { public: a(){ cout<<"inside constructor of a"<<endl; } int b()
8
2504
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...
33
2986
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...
3
22779
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
2547
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,...
10
4171
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: ...
9
2981
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
29293
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...
4
10886
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...
0
7041
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
6908
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
7043
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
7081
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
6921
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
5336
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,...
1
4776
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...
0
2995
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...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.