swig模板上的未定义符号与python中的静态成员

时间:2011-03-19 12:35:59

标签: c++ python templates swig static-members

需要使用swig导出一些模板化类的一部分,并带有一些静态成员。 所有编译都很好,并使用名称_pipeit.so正确创建模块;问题来自python我执行import pipeit因为它给出以下错误:

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

问题是否来自模板中静态成员的使用?如果是这样,他们应该如何处理?

这里是swig接口文件的代码:

%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 >;
}

工厂,配置和生成器类也包含在接口文件中,我不会在这里报告它们的简短性,我也没有报告所有其他所需的#includes和异常处理。

以下是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;

}    

有人可以帮助我吗?

谢谢 卢卡

编辑: 遵循建筑说明:

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

哦,我错过了说我的模板化FactoriesPool类(和所有其他类一样)包含在我的共享库libpipeit中。

无论如何,我认为这不是构建命令的问题,因为我的接口文件包含许多其他类,其中一些也是模板化的,并且在我尝试导出最后一个模板的类之前一切正常。包含一些静态成员。

1 个答案:

答案 0 :(得分:1)

析构函数~FactoriesPool()在哪里定义?运行时链接程序正在寻找它。