Thread: Exporting template from dll

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Exporting template from dll

    I know it's been discussed somewhere before, but I can't find it.
    I don't quite remember how to export templates from a dll.
    My current code does:

    Code:
    template<typename T> class AFX_EXT_CLASS CTmplString
    And:

    Code:
    EXPORT template CTmplString<char>;
    EXPORT template CTmplString<wchar_t>;
    EXPORT is defined as:

    Code:
    #ifdef _AFXEXT
    #define EXPORT __declspec(dllexport)
    #else
    #define EXPORT __declspec(dllimport)
    #endif
    Yet, I get a heap of linker errors for CTmplString<wchar_t>.
    Anyone got a pointer? It's supposed to work to export explicit instantiations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Have you tried creating typedefs and then exporting the typedefs?

    Edit: Wait, don't you need to do this to specialize a template?
    Code:
    template<> CTmplString<char>;

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What I've done in the past is just look at code that already does this. For example, CString, CStringA, and CStringW are all explorted template instanciations of CStringT<> in the latest MFC.

    You can also look at exported STL instanciations like cout, cerr - as well as template instanciations like string and wstring.

    These may be usefull as well:
    http://support.microsoft.com/kb/168958
    http://msdn.microsoft.com/en-us/libr...86(VS.80).aspx
    http://support.microsoft.com/kb/309801/en-us

    gg

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Alright! Finally, it's working.
    The code is actually:

    Code:
    template class __declspec(dllexport) Strings::CTmplStringBase<wchar_t>;
    And similarly,
    Code:
    template class __declspec(dllimport) Strings::CTmplStringBase<wchar_t>;
    And my biggest problem is that I separate definition and implementation of classes, even template classes, so naturally I was spammed with errors that it couldn't export due to that it couldn't find the implentation.
    And if I put the export/import after the implementation was done, then the program would never import it.

    Finally tracked down the problem and now it works fine.
    Now I just have to nail those stupid Ansi/Wide issues.
    Probably on a similar way as to how MFC did it...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM