Thread: Templates from DLL or static library problem

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Templates from DLL or static library problem

    Hi, I was trying to store a templated function in a DLL to use it in some other app, but after
    getting linker errors, I figured out that we can't put templates in DLL because the function
    doesn't compile itself in the DLL, some says it's only a pattern to tell how to create new
    functions, or so I heard...

    While I was googling, I saw that even if templates on DLLs don't work, it should be possible
    to achieve it with static libs files only, no DLL... So I made a test in a static library, but I still
    got linker error. I though that static libs would simply work as if it was hidden uncompiled code. So... is there any way to have externally used templates with libs?

    Code:
    //Lib project source
    template <class X>
    void TestFunc(X var)
    {
    	//Deal with var
    }
    
    //Test application
    #pragma comment(lib, "Test.lib")
    int main(void)
    {
    	TestFunc<int>(123);
    	return 0;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In short, you can't go as far as you want to. The best option is to just leave the template implementation in header files, and include that in each project.

    You can abstract out the common parts between the different types and put that in a lib, and then have template functions make use of the lib.
    For example, this is much the same as if one were to implement say std::sort for a vector by implementing a template wrapper function that uses C's qsort.
    I believe there is an STL implemention out there that does that kind of thing and claims to reduce code bloat somewhat. Can't remember the details though.
    In general it wont be able to be optimised as well.

    You other option is to force it to compile a template instantiation for each type of interest, by actually using the functions for each type in the lib, and then I believe they can be exported. That wont then work for other types though, of course.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Ok, I get it. Then I will just leave them in header files with the code. Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static data structure in a library
    By melight in forum C Programming
    Replies: 6
    Last Post: 01-10-2009, 11:12 AM
  2. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. DLL Function / Load Library Problem
    By cboard_member in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2005, 10:11 AM
  5. static variable in templates
    By arjunajay in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 08:44 AM