簡體   English   中英

在python中帶有靜態成員的模板上使用未定義符號

[英]swig undefined symbol on templates with static members in python

需要使用Swig將帶有一些靜態成員的模板化類的一部分導出到python。 所有代碼都可以很好地編譯,並且使用_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和異常處理。

這是factory.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中。

無論如何,我認為構建命令不是問題,因為我的接口文件包含許多其他類,其中一些也已模板化,並且在我嘗試導出最后一個已模板化的類之前也都工作良好,並且還包含一些靜態類。成員。

析構函數〜FactoriesPool()在哪里定義? 運行時鏈接程序正在尋找它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM