Two instances of a static member, how could this be?

I have a multithreaded application. I declare a class with a static member in a shared library.

Printing member addresses from different streams from different libraries shows different results.

//announcement

template <class OBJECT>
struct Container
{
   static int m_member;
};

template <class OBJECT>
int Container<OBJECT>::m_member;

// print

cout << (void*) &Container<int>::m_member << endl;

How could this be?

+5
source share
1 answer

If you have different libraries (I guess different dynamic libraries), then you may have some duplication of both code and static variables.

The exact details will depend on the particular dynamic library technology you are using. I would say that, for example, in the Windows DLL you will have duplicated code and variables, but in Linux SOs you will not.

, .

UPDATE: , - ! - ! , , , SO . , , std::string, :

:

namespace std
{
    extern template class basic_string<wchar_t>;
}

:

namespace std
{
    template class basic_string<wchar_t>;
}

, , . , SO , , .

UPDATE: , , ... , extern, ELF .

. . , -fvisibility=hidden . , :

template <class OBJECT>
struct __attribute__((visibility("default"))) Container
{
   static int m_member;
};

, , .

+4

All Articles