Boost logo

Boost :

From: Csaba Szepesvari (szepes_at_[hidden])
Date: 2000-10-20 04:49:21


Would not the example still hold without any reference to shared_ptr?? (see
code below)
Given what you were saying I would think it would hold (i.e., would fail).

Conclusion is not to export classes with template members - yes, I'd expect
this behavior given the limits of DLLs anyway. I would never try to export
*any* templates from a DLL:

1) the MSVC compiler seems to require to see all the template code when it
needs to instantiate one
2) the MSVC compiler requires that "exported" template code go to headers
(exporting of template stuff is not supported)

Also, DLLs are limited in a number of other ways (i.e., memory allocation
should consistently happen within the DLL xor the client).
Best to think of Win32 DLLs as being invented for C.

- Csaba
------------------------------------------------------------
code follows:
------------------------------------------------------------

#ifdef BUILD_EXAMPLE_DLL
#define EXAMPLE_EXPORT __declspec(dllexport)
#else
#define EXAMPLE_EXPORT __declspec(dllimport)
#endif

class EXAMPLE_EXPORT Example
{
public:
        Example();
        template <class X> void print( const X& x ) { std::cout <<
x; }
};

======== EOF ====

==== Application.cpp ======

#include <ExampleClass.hpp>

int main()
{
        Example e;
        e.print(3);
        return 0;
}

- Csaba

"Schaible, Joerg" wrote:

> Hi David,
>
> > Sorry, I don't understand. Since all template member
> > functions are inline in
> > MSVC, how can they interact with anything at link time?
>
> Just a little example hacked down here into the mail to illustrate the
> problem for MSVC.
>
> Code that will be provided in an DLL:
>
> ==== ExampleClass.hpp ======
>
> #ifdef BUILD_EXAMPLE_DLL
> #define EXAMPLE_EXPORT __declspec(dllexport)
> #else
> #define EXAMPLE_EXPORT __declspec(dllimport)
> #endif
>
> #include <boost/smart_ptr.hpp>
>
> class EXAMPLE_EXPORT InternalHelper
> {
> };
>
> // Have to do export the instanciated template for MSVC to link the DLL
> extern template class EXAMPLE_EXPORT boost::shared_ptr<InternalHelper>;
>
> class EXPORT Example
> {
> boost::shared_ptr<InternalHelper> sptrHelper;
> public:
> Example();
> template <class X> void print( const X& x ) { std::cout << x; }
> };
>
> ======== EOF ====
>
> I omit the .cpp. Now I show you the usage of this class from an application
> using the DLL.
>
> ==== Application.cpp ======
>
> #include <ExampleClass.hpp>
>
> int main()
> {
> Example e;
> e.print(3);
> return 0;
> }
>
> ======== EOF ====
>
> This will result in a linker error, if my code in the DLL never use this
> instanciation of the member with an int. MSVC expects *every* instanciation
> of the member template function coming out of that DLL and that is not
> possible!
>
> Greetings,
> Jörg


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk