Binary interfaces in component development

A template class with static data - how can that possibly go wrong?

Secure remote control for conventional and virtual desktops

Clearly, the linker has no chance to resolve multiple definitions of a single symbol if the symbol name is not present to connect all the different instances together. As a first step to resolving this problem, we’ll use linkage specifiers to bring our data into the export, to see if that helps solve the problem.

However, in Microsoft Visual Studio there exists a mechanism to tag a definition that will be exported from a dll. This tag causes the linker to put the symbol name of the definition into the dll export table so that other binaries using the dll can find the definition. A second tag allows you to import definitions from a dll, signalling to the linker not to search for the definition at link time but instead to use an indirection which will be replaced at runtime by the real definition, in a process known as 'fixup' (see also here). In code, you typically see a system of macros to manage this tagging. Declarations that form the interface of the dynamic library are marked with this macro and when building on windows the preprocessor substitute the macro for ‘__declspec(dllexport)’ indicating to the linker that the symbol should be placed in the export table for use by other binaries linking to the library. When building a binary that uses a dll, the same macros expand to ‘__declspec(dllimport)’ indicating to the linker that the needed definition will be available at runtime if it’s not found. There’s one problem with this system, however. It works reasonably well when the binary definition is located in the library corresponding to the C++ class definition; but in a world of templates, the template definition could be located in some library far, far away, although its instantiation is in the library that you’re building.

If you have control over the template definition, you may be able to get past this with an increasingly messy system of macros as illustrated in the code below, but if the template definition is from a third party library then the only option is to hide any template instantiations far away from the interfaces of the dynamic library.

So, by adding a rather elaborate system of macros (check the code for this figure if you’re interested), we’re able to get our static data into the exports table. Just to clarify, I’m not recommending the usage of such a system of macros; it’s simply a way to get the static variable resulting from the instantiation of a template from another library exported. Have these efforts solved the problem though? Unfortunately not, as the following screenshot (Figure 5) reveals; depending on which dll the calling code is in. The static variable GenericSingleton<MySingletonObject>::m_instance is located at a different memory address.

Figure 5: Shows one static variable with 2 locations.

In our case, an important difference means this system will not work. Our class template is not defined in our dll so its definition is not tagged with the macro that expands to dllexport. Using a modified mechanism, we were able to export the symbol, but as both dlls needed to instantiate the variable and type, this still didn’t resolve the issue.

Choosing a cloud hosting partner with confidence

More from The Register

next story
Microsoft on the Threshold of a new name for Windows next week
Rebranded OS reportedly set to be flung open by Redmond
Apple: SO sorry for the iOS 8.0.1 UPDATE BUNGLE HORROR
Apple kills 'upgrade'. Hey, Microsoft. You sure you want to be like these guys?
SMASH the Bash bug! Apple and Red Hat scramble for patch batches
'Applying multiple security updates is extremely difficult'
Business is back, baby! Hasta la VISTA, Win 8... Oh, yeah, Windows 9
Forget touchscreen millennials, Microsoft goes for mouse crowd
ARM gives Internet of Things a piece of its mind – the Cortex-M7
32-bit core packs some DSP for VIP IoT CPU LOL
Lotus Notes inventor Ozzie invents app to talk to people on your phone
Imagine that. Startup floats with voice collab app for Win iPhone
prev story


Providing a secure and efficient Helpdesk
A single remote control platform for user support is be key to providing an efficient helpdesk. Retain full control over the way in which screen and keystroke data is transmitted.
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.
Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.