Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:26:39+00:00 2026-06-05T15:26:39+00:00

There were many questions about C++ template classes which contain static member variables, as

  • 0

There were many questions about C++ template classes which contain static member variables, as well as about exporting them from dynamic libraries or shared objects. But this one is a bit deeper: what to do if there are multiple shared objects, each of them having its own set of instantiations but possibly using instantiations from another shared object?

Consider the following example code:

/* file: common.h */

#include <stdio.h>
#define PRINT fprintf (stderr, "(template %d) %d -> %d\n", parameter, data, new_data)

template <int parameter>
class SharedClass
{
    static int data;
public:
    static void Set(int new_data) { PRINT; data = new_data; }
};

template <int parameter>
int SharedClass<parameter>::data = parameter;


/* file: library1.h */

extern template class SharedClass<1>;
void Library1Function();


/* file: library1.cpp */

#include "common.h"
#include "library1.h"
#include "library2.h"

template class SharedClass<1>;

void Library1Function()
{
    SharedClass<1>::Set (100);
    SharedClass<2>::Set (200);
}


/* file: library2.h */

extern template class SharedClass<2>;
void Library2Function();


/* file: library2.cpp */

#include "common.h"
#include "library1.h"
#include "library2.h"

template class SharedClass<2>;

void Library2Function()
{
    SharedClass<1>::Set (1000);
    SharedClass<2>::Set (2000);
}


/* file: main.cpp */

#include "common.h"
#include "library1.h"
#include "library2.h"

int main()
{
    Library1Function();
    Library2Function();
    SharedClass<1>::Set (-1);
    SharedClass<2>::Set (-2);
}

Let’s then assume we build the two libraries and an application using GCC:

$ g++ -fPIC -fvisibility=default -shared library1.cpp -o lib1.so
$ g++ -fPIC -fvisibility=default -shared library2.cpp -o lib2.so
$ g++ -fvisibility=default main.cpp -o main -Wl,-rpath=. -L. -l1 -l2

And then run the executable, we’ll get the following result:

$ ./main
(template 1) 1 -> 100
(template 2) 2 -> 200
(template 1) 100 -> 1000
(template 2) 200 -> 2000
(template 1) 1000 -> -1
(template 2) 2000 -> -2

Which means that both libraries and the executable access the same per-template static storage.
If we run “nm -C” on the binaries, we’ll see that each static member is defined only once and in the corresponding library:

$ nm -C -A *.so main | grep ::data
lib1.so:0000000000001c30 u SharedClass<1>::data
lib2.so:0000000000001c30 u SharedClass<2>::data

But I’ve got some questions.

  1. Why, if we remove the extern template class ... from both headers, we’ll see that the static members are present in each binary, but the test application will continue to work properly?

    $ nm -C -A *.so main | grep ::data
    lib1.so:0000000000001c90 u SharedClass<1>::data
    lib1.so:0000000000001c94 u SharedClass<2>::data
    lib2.so:0000000000001c94 u SharedClass<1>::data
    lib2.so:0000000000001c90 u SharedClass<2>::data
    main:0000000000401e48 u SharedClass<1>::data
    main:0000000000401e4c u SharedClass<2>::data
    
  2. Is it possible to build this under MSVC?
    Or, more specifically, how to deal with __declspec(dllexport) and __declspec(dllimport) to make some instantiations exported, and some – imported?

  3. And, finally: is this an example of undefined behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:26:42+00:00Added an answer on June 5, 2026 at 3:26 pm

    To answer point 1: When the dynamic linker resolves symbols, it uses a list of modules to link against. The first module loaded is checked first, then the second, and so on.

    IIRC, when the data member is used in main, lib1.so, and lib2.so, this is still treated as a dynamic symbol reference, even though the member is declared in the same module. So when the linker goes to resolve the symbols when you run the program, all three modules wind up using the data member implementation in just one of the three modules: whichever was loaded first. The other two pairs are still loaded into memory, but are unused.

    (Try std::cout << &(SharedClass<n>::data) << std::endl in all three modules; the address printed should be the same for all six cases.)

    To answer point 3, I don’t believe this behavior is undefined at all. What happens exactly depends on your system’s dynamic linker, but I don’t know of any linker that wouldn’t handle this situation in exactly the same way.

    I can’t speak to point 2 since I don’t have a whole lot of experience with MSVC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that there are many existing questions about Django template images, but I
I know that there are many questions about this but I cannot make it
I know that there are many different questions about this sort of topic on
There are many questions on StackOverflow about simple, database-less login systems. I was about
NOTE: I know there are many questions that talked about that but I'm still
There seem to be many questions asked about this subject here on stackoverflow, but
I know there are a lot of questions about this, and I've tried many
There are many questions like this, but none of them seem to answer my
I know there are many questions about jQuery + Back button issues, but it
I know there are many questions about jQuery slideToggle flickering but I haven't seen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.