Compile header-only template library into a shared library?

后端 未结 4 1470
情深已故
情深已故 2020-12-15 11:11

We are in the process of designing a new C++ library and decided to go with a template-based approach along with some specific partial template specialisations for corner ca

4条回答
  •  太阳男子
    2020-12-15 11:46

    Although there isn't a standard way to do it, it is usually possible with implementation specific techniques. I did it a long time ago with Borland's C++ Builder. The idea is to declare your templates to be exported from the shared library where they need to reside and import them where they are used. The way I did it was along these lines:

    // A.h
    #ifdef GENERATE
    #  define DECL __declspec(dllexport)
    #else
    #  define DECL __declspec(dllimport)
    #endif
    
    template  class DECL C {
    };
    
    // A.cpp
    #define GENERATE
    #include "A.h"
    
    template class DECL A;
    

    Beware that I don't have access to the original code, so it may contain mistakes. This blog entry describes a very similar approach.

    From your wording I suspect you're not on Windows, so you'll have to find out if and how this approach can be adopted with your compiler. I hope this is enough to put you in the right direction.

提交回复
热议问题