简体   繁体   中英

swig undefined symbol on templates with static members in python

The need is to export to python with swig a portion of a templated class with some static members. All compiles well and the module is correctly created with the name _pipeit.so; the problem comes when from python i execute import pipeit as it gives the following error:

ImportError: ./_pipeit.so: undefined symbol: _ZN8gestface13FactoriesPoolINS_9GeneratorEED1Ev

Does the problem is given from the usage of static members in a template? If so how they should be handled?

here follows the code of the swig interface file:

%module pipeit
%include "std_string.i"

%{
    #define SWIG_FILE_WITH_INIT
    #include "factory/factories.h"
%}


namespace gestface{
%newobject FactoriesPool::build;

template<typename BuildedT>
class FactoriesPool{
private:
    FactoriesPool(){}
    FactoriesPool(const FactoriesPool& b);
    FactoriesPool& operator=(const FactoriesPool& b);

    std::map< std::string, Factory<BuildedT>* > factory_map;
    static FactoriesPool<BuildedT>* singleton_instance;
public:
    ~FactoriesPool();
    static const ConfigurationTemplate& get_configuration_template(const std::string& class_name) throw(bad_class);
    static BuildedT* build( const std::string& class_name,
                            const Configuration& conf = Configuration() ) throw(bad_parameter, bad_class);
};

%template( GeneratorFactoriesPool ) FactoriesPool< Generator >;
}

Factory, Configuration and Generator classes are included as well in the interface file, i don't report them here for shortness and i don't report all other needed #includes and exception handling for the same reason.

and here is the code of the class in the factories.h:

namespace gestface{

template<typename BuildedT>
class FactoriesPool{
private:
    FactoriesPool(){}
    FactoriesPool(const FactoriesPool& b);
    FactoriesPool& operator=(const FactoriesPool& b);

    std::map< std::string, Factory<BuildedT>* > factory_map;
    static FactoriesPool<BuildedT>* singleton_instance;

public:
    typedef BuildedT builded_t;
    ~FactoriesPool();

    void add_factory(const std::string& class_name, Factory<BuildedT>* factory){
        Factory<BuildedT>* f = factory_map[class_name];
        if(f) delete f;
        factory_map[class_name] = factory;
    }

    static FactoriesPool<BuildedT>* get_instance(){
        if(!singleton_instance) singleton_instance = new FactoriesPool<BuildedT>();
        return singleton_instance;
    }

    static const ConfigurationTemplate& get_configuration_template(const std::string& class_name) throw(bad_class){
        if(!singleton_instance) singleton_instance = new FactoriesPool<BuildedT>();
        const Factory<BuildedT>* f = singleton_instance->factory_map[class_name];
        if(!f){
            std::stringstream ss;
            ss << "No such class: " << class_name;
            throw bad_class(ss.str());
        }
        const ConfigurableFactory<BuildedT>* cf = dynamic_cast< const ConfigurableFactory<BuildedT>* >(f);
        if(!cf){
            std::stringstream ss;
            ss << "Not configurable: " << class_name;
            throw bad_class(ss.str());
        }

        return cf->get_configuration_template();
    }

    static BuildedT* build( const std::string& class_name,
                            const Configuration& conf = Configuration() ) throw(bad_parameter, bad_class){
        if(!singleton_instance) singleton_instance = new FactoriesPool<BuildedT>();
        const Factory<BuildedT>* f = singleton_instance->factory_map[class_name];
        if(!f){
            std::stringstream ss;
            ss << "No such class: " << class_name;
            throw bad_class(ss.str());
        }

        BuildedT* g;
        if(conf.get_template().parameters_num() != 0){
            const ConfigurableFactory<BuildedT>* cf = dynamic_cast< const ConfigurableFactory<BuildedT>* >(f);
            if(!cf){
                std::stringstream ss;
                ss << "Not configurable: " << class_name;
                throw bad_class(ss.str());
            }
            g = cf->build(conf);
        }else {
            g = f->build();
        }
        return g;
        }
};

template<typename BuildedT>
FactoriesPool<BuildedT>* FactoriesPool<BuildedT>::singleton_instance = 0;

}    

Does anyone can help me?

Thank you Luca

EDIT: Here follow building instructions:

swig -Wall -c++ -python -o pipeit_wrap.cpp -outdir dist/Debug pipeit.i
g++ -c -g -ansi -I../../include -I/usr/include/python2.6 -fPIC -MMD -MP -MF build/Debug/pipeit_wrap.o.d pipeit_wrap.cpp -o build/Debug/pipeit_wrap.o
g++ -g -ansi -o dist/Debug/_pipeit.so build/Debug/pipeit_wrap.o -L../../dist/Debug/GNU-Linux-x86 -shared  -lboost_system -lpipeit

Oh, and i missed to say that my templated FactoriesPool class (as all others) is contained in my shared library libpipeit.

Anyway i think it is not a problem of building commands, because my the interface file contains a number of other classes, some of them templated as well and all worked fine before i tried to export this last class that is templated and also contains some static members.

Where is the destructor ~FactoriesPool() defined? The runtime linker is looking for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM