Connecting Tech Pros Worldwide Forums | Help | Site Map

Global variable initialization using LIB project

Marco Segurini
Guest
 
Posts: n/a
#1: Nov 17 '05
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



Closed Thread