Suppose I have a singleton class S in a static library, this could be linked with the other dynamic libraries D1 D2 D3,
So from what I understand the class S will have a separate instance in each D1, D2 and D3 and this would be true even if it is not a singleton (like global)
Is there any way to prevent multiple copies of class S? I cannot put the singleton S in another Dynamic library.
Executable
/ | \ \
D1 D2 D3 D4
| | |
S S S
EDIT:
The singleton S is in a separate static library that links with D1 D2 D3... separately.
The singleton is allocated in the heap, only the pointer is static
static s::instance()
{
static smart_ptr<S> ptr = NULL;
if(ptr == NULL) ptr = new S;
return ptr;
}
Edit2:
I made a simple test case to check things out This is a sample makefile (replace .dll by .so) i made to check things out, I checked it on Ubuntu and Cygwin, both g++ compilers and the behaviour was different. cygwin created 2 different objects but but ubuntu created 1 objects
all: dynamic1 dynamic2 main
static: static.cpp
g++ -c -fPIC static.cpp -o obj/static.o
ar rvs lib/static.a obj/static.o
dynamic1: static dynamic1.cpp
g++ -fPIC -shared dynamic1.cpp lib/static.a -o lib/libdynamic1.dll
dynamic2: static dynamic2.cpp
g++ -fPIC -shared dynamic2.cpp lib/static.a -o lib/libdynamic2.dll
main: dynamic1 dynamic2 main.cpp
g++ --std=c++11 main.cpp -ldynamic1 -ldynamic2 -o lib/main -L./lib