Jump to content
  • Advertisement
Sign in to follow this  
Integra

DLL hell with templates, help me out!

This topic is 5396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok I'm having a problem with templates and DLLs, and I was hoping somebody could maybe direct me to a good paper on the problem. I've read a bunch of articles, but I pretty much only found hackish solutions that I don't want to use. Basically my problem is when I create a templated class for my engine (which compiles to a DLL), such as a Singleton, Iterator, etc. The code compiles fine, but there are linker errors which I believe are because with the DLL it does not instantiate properly. Basically if I were to use the singleton templated class such as: class WhateverManager : public Singleton< WhateverManager > I get linker errors for undefined symbols, can anybody lend a hand, or an article? :) EDIT: oh and also, I'm not calling this templated class from outside of the DLL, I am strictly using it for internal use.

Share this post


Link to post
Share on other sites
Advertisement
people always do this :P !!!!

you need to give the specific errors and as much code as possible.

come back with that, i'll be waiting ;)

Share this post


Link to post
Share on other sites
That happens a lot when you forget to inline the whole template in the header, but yeah, without code it's hard to say... I'm pretty sure I've gotten that to work before though.

Share this post


Link to post
Share on other sites
Well as I said this is not a code problem, and it is actually a fairly common problem with DLLs, I just can't find a good paper on the solution.

It's a linker only error, and happens because when you compile to a DLL with templates it doesn't instantiate properly.

The specific errors are just unknown symbols due to the template not instantiating properly, and the code is irrelevant as it compiles perfectly fine, and can be instantiated in exe's (as I've tested). This only happens in DLLs, and is a common problem I guess.


EDIT: hmm, I'll go make sure I have the template fully inlined in the header. ( I just checked and it is fully inlined ).

Share this post


Link to post
Share on other sites
Unfortunately, you wont be able to use the DLL export mechanisms to share any of your template code. In order to make it work, you would have to manually instantiate any classes in your DLL that you wish to export. Which means your DLL would have to know about the classes that will use it, which sort of defeats the purpose of using a DLL.

The solution is to make the template code completely exposed via header files, so client code will have their own instantiations, and to ensure any communication between the templates and the shared data in the DLL happen via ordinary exported functions.

- Phil

Share this post


Link to post
Share on other sites
Quote:
Original post by Integra
EDIT: oh and also, I'm not calling this templated class from outside of the DLL, I am strictly using it for internal use.


This wasn't here when I first replied :P

Since its all internal, could you post the specific linker errors please, and the code that they're pointing to?

Share this post


Link to post
Share on other sites
Quote:
Original post by Phillip Martin
Quote:
Original post by Integra
EDIT: oh and also, I'm not calling this templated class from outside of the DLL, I am strictly using it for internal use.


This wasn't here when I first replied :P

Since its all internal, could you post the specific linker errors please, and the code that they're pointing to?


See, See, I'm not crazy!!! :)

Share this post


Link to post
Share on other sites
Well the reason I haven't posted the code is because it is the Singleton class from Game Programming Gems and I'm unsure of the legalities of posting that code here. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Integra
Well the reason I haven't posted the code is because it is the Singleton class from Game Programming Gems and I'm unsure of the legalities of posting that code here. :)


Well just plonk up the linker errors then, I'd be happy with that. I'd be taking stabs in the dark otherwise, and no-one likes getting a knife in their eye while looking for candles.

Share this post


Link to post
Share on other sites
Here is the error I am currently getting with the linker:

Error 6 error LNK2001: unresolved external symbol "protected: static class Specter::CLogFile * Specter::CSingleton<class Specter::CLogFile>::ms_Singleton" (?ms_Singleton@?$CSingleton@VCLogFile@Specter@@@Specter@@1PAVCLogFile@2@A) Specter


In the CSingleton class you can find:

protected:
static T *ms_Singleton;


I create the CLogFile class like this:

class CLogFile : public CSingleton< CLogFile >

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!