[C++-sig] [boost.python] Can't import a wrapped template class

Paul O. Seidon p.oseidon at datec.at
Fri Oct 26 20:16:53 CEST 2012


Stefan Seefeld wrote:

> On 10/26/2012 01:50 PM, Paul O. Seidon wrote:
>> The ctor is decl'ed in varbls.h as
>>
>> _Variable();
>>
>> and it's def'ed in varbls.cpp like so:
>>
>> template <typename TYPE>
>> _Variable<TYPE>::_Variable()
>> : _value( 0)
>> {
>>      //ctor
>> }
> 
> That doesn't work. When the compiler compiles varbls.cpp, it doesn't
> know what types to instantiate the _Variable template for, so you need
> to either explicitly instantiate it for all the types you use in your
> module, or keep the definitions in the varbls.h header so the compiler
> can implicitly instantiate them when compiling the Python module.
> 
>     Stefan
> 

That didn't make any difference. And it would have surprised me, if it did: 
varbls.h contains the declaration, varbls.cpp contains the definition, the 
wrapper is in main.cpp. So if the compiler sees any need to include 
sometihng, it will and the linker will link the definition.

But you certainly are right, the symbol is in the .so-file but the symbol 
isn't resolved by the linker. Hm, but where should I instantiate the class? 
I thought 

BOOST_PYTHON_MODULE(_varbls)
{

     class_<VariableDouble>("VariableDouble")
         .def( init<>())
         .def( init<const double&>())
     ;
}

would do that. Should I try

dont_care = _Variable<double>();

in main.cpp? Looks a bit strange, but would force the compiler to generate 
code for sure.

Paul



More information about the Cplusplus-sig mailing list