在共享库中为多个C ++文件创建Python接口时出错

时间:2019-01-29 08:45:41

标签: boost-python

我有两个名为“ animal.cpp”和“ zoo.cpp”的c ++文件。这些文件被编译为共享库“ zoo.so”。我想从Python解释器访问这些文件中定义的功能。

但是,我遇到了错误。我尝试进行一些更改,但得到了不同类型的错误。我希望Boost-Python的专家可以指导我解决这些问题。

注意:为了使问题更简单,我没有使用过类,否则我可以轻松地将“动物”和“动物”类设为

我想从Python访问这两个文件的功能。

任何指导将不胜感激。

谢谢

animal.cpp

+++++++++++

/*
 * This is the C++ function we write and want to expose to Python.
 */

const std::string hello_animal() {
    return std::string("hello, animal");
}

/*
 * This is the C++ function we write and want to expose to Python.
*/
const std::string getname_animal() {
    std::string input;
    std::cout<<"Please enter your favorit animal name: ";
    std::getline(std::cin,input);
    return std::string("Your favorit animal name is: ").append(input);
}

/*
 * This is a macro Boost.Python provides to signify a Python extension module.
 */
BOOST_PYTHON_MODULE(animal) {
    // An established convention for using boost.python.
    using namespace boost::python;

    // Expose the function hello_animal().
    def("hello_animal", hello_animal);

    // Expose the function getname_animal().    
    def("getname_animal", getname_animal);
}


zoo.cpp
++++++++

/*
 * This is the C++ function we write and want to expose to Python.
 */
const std::string hello_zoo() {
    return std::string("hello, zoo");
}

/*
 * This is the C++ function we write and want to expose to Python.
*/
const std::string getname_zoo() {
    std::string input;
    std::cout<<"Please enter your favorit zoo name: ";
    std::getline(std::cin,input);
    return std::string("Your favorit zoo name is: ").append(input);
}

/*
 * This is a macro Boost.Python provides to signify a Python extension module.
 */
BOOST_PYTHON_MODULE(zoo) {
    // An established convention for using boost.python.
    using namespace boost::python;

    // Expose the function hello_zoo().
    def("hello_zoo", hello_zoo);

    // Expose the function getname_zoo().    
    def("getname_zoo", getname_zoo);
}

我使用以下命令创建了共享库:

g ++ -shared -o zoo.so -fPIC zoo.cpp animal.cpp -lboost_python -lpython2.7 -I / usr / include / python2.7

Python解释器命令和输出:

  
    
      

导入动物园

             

zoo.getname_zoo()

    
  

请输入您喜欢的动物园名称:动物园

“您最喜欢的动物园名称是:动物园”

  
    
      

zoo.getname_animal()

    
  

回溯(最近通话最近):   文件“”,第1行,位于 AttributeError:“模块”对象没有属性“ getname_animal”

当我在animal.cpp文件中进行以下更改时,出现了不同的错误。

BOOST_PYTHON_MODULE(zoo){

代替

BOOST_PYTHON_MODULE(动物){

编译时出现以下错误:

[root @ localhost zooexample]#g ++ -shared -o zoo.so -fPIC zoo.cpp animal.cpp -lboost_python -lpython2.7 -I / usr / include / python2.7 /tmp/cci4WKrP.o:在函数initzoo': animal.cpp:(.text+0x15d): multiple definition of initzoo'中 /tmp/cctaGDer.o:zoo.cpp:(.text+0x15d):首先在此处定义 /tmp/cci4WKrP.o:在函数init_module_zoo()': animal.cpp:(.text+0x179): multiple definition of init_module_zoo()中 /tmp/cctaGDer.o:zoo.cpp:(.text+0x179):首先在此处定义 collect2:错误:ld返回1退出状态

++++++++++++++++++++++++++++++ UPDATED ++++ UPDATED +++++ UPDATED +++++++++++++++

考虑Dan对我上一期的建议。现在,我创建了一个单独的文件pyintf.cpp,其中包含以下代码:

BOOST_PYTHON_MODULE(pyintf) {
    def("hello_zoo", hello_zoo);
    def("getname_zoo", getname_zoo);
    def("hello_animal", hello_animal);
    def("getname_animal", getname_animal);
} 

但是当我使用以下命令编译并创建共享库时:

g ++ -shared -o pyintf.so -fPIC pyintf.cpp -lboost_python -lpython2.7 -I / usr / include / python2.7

在python中导入“ pyintf”模块时出现以下错误。

  
    
      

导入pyintf       追溯(最近一次通话):         文件“”,第1行,位于       ImportError:./pyintf.so:未定义符号:_Z9hello_zoov

    
  

谁能告诉我我做错了什么?

++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++

1 个答案:

答案 0 :(得分:0)

通过将所有我想公开的方法都放在一个pyintf.cpp中,解决了这些问题。更新后出现的错误是由于缺少头文件和源文件。

现在,通过在编译命令中添加头文件(zoo.h,animal.h)和源文件(zoo.cpp,animal.cpp),解决了上述问题。 g ++ -shared -o pyintf.so -fPIC pyintf.cpp animal.h animal.cpp zoo.h zoo.cpp -lboost_python -lpython2.7 -I / usr / include / python2.7

感谢Dan的更正。