boost-python嵌套名称空间导出代码中的错误

问题描述:

我正在努力导出两个类的动物园(extzoo和intzoo 命名空间),类动物(extanim和intanim命名空间)"方法是 在两个级别的嵌套名称空间中定义.我想公开这些方法 从我可以访问它们的地方到Python解释器.我已经写了代码, 创建了一个共享库,但是当我将其导入python时出现错误. 感谢您对此的指导.

I am working on exporting two classes class zoo (extzoo and intzoo namespaces), class animal (extanim and intanim namespaces)" methods which are defined within two level nested namespaces. I want to expose these methods to Python interpreter from where I can access them. I have written code, created a shared library but when I import it into a python I get an error. I will appreciate your guidance on this.

在类似的情况下,我遵循了以下链接上给出的答案 C ++嵌套名称空间的说明:创建boost-python嵌套名称空间

I have followed the answer given on the following link in similar context of exposing C++ nested namespaces: create boost-python nested namespace

zoo.h:

namespace extzoo
{
namespace intzoo
{
class zoo
{
public:
const std::string hello_zoo();
const std::string getname_zoo();
};
}
}      

zoo.cpp:

using namespace extzoo;
using namespace intzoo;

const std::string zoo::hello_zoo() {
     return std::string("hello, zoo");
}

const std::string zoo::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);
}

animal.h:

namespace extanim
{
namespace intanim
{
class animal
{
public:
const std::string hello_animal();     
const std::string getname_animal();
};
}
}

animal.cpp:

animal.cpp:

using namespace extanim;
using namespace intanim;

const std::string animal::hello_animal() {
      return std::string("hello, animal");
}
const std::string animal::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);
     }

pyintf.cpp:

pyintf.cpp:

// An established convention for using boost.python.
using namespace boost::python;
//user defined ns
using namespace extzoo::intzoo;
using namespace extzoo;
using namespace extanim::intanim;
using namespace extanim;

class DummyZoo{};
class DummyAnimal{};

BOOST_PYTHON_MODULE(pyintf) {

    scope intzoo
    = class_<DummyZoo>("intzoo");

    class_<extzoo::intzoo::zoo>("zoo")
    // Expose the function hello_zoo().
       .def("hello_zoo", &extzoo::intzoo::zoo::hello_zoo)
        // Expose the function getname_zoo().    
        .def("getname_zoo", &extzoo::intzoo::zoo::getname_zoo)
        ;



    scope intanim
        = class_<DummyAnimal>("intanim"); 

    class_<extanim::intanim::animal>("animal")
        // Expose the function hello_animal().
        .def("hello_animal", &extanim::intanim::animal::hello_animal)
        // Expose the function getname_animal().    
        .def("getname_animal", &extanim::intanim::animal::getname_animal)
        ;
}

我已经使用以下命令编译了上面的代码:

I have compiled the above code using following command:

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

导入共享库时出现错误

导入pyintf 追溯(最近一次通话): 文件",第1行,在 ImportError:./pyintf.so:未定义的符号:_ZN7extanim7intanim6animal14getname_animalEv

import pyintf Traceback (most recent call last): File "", line 1, in ImportError: ./pyintf.so: undefined symbol: _ZN7extanim7intanim6animal14getname_animalEv

++++++++++++++++++++++++++ Update +++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++Update+++++++++++++++++++++++++++++++++

我已根据@doqtor建议更改了代码.但是,现在我无法在Python中导入在一个文件中定义的名称空间"intanim",而其他文件可以"intzoo".您可能会在下面看到.

I have changed the code based on @doqtor suggestion. However, now I cannot import in Python a namespace 'intanim' defined in one file while other can 'intzoo'. As you may see below.

>>> import pyintf 
>>> pyintf.intanim.animal().hello_animal()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'intanim'
>>> from pyintf import intanim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name intanim
>>> from pyintf import extanim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name extanim
>>> from pyintf import intzoo
>>> intzoo.zoo().hello_zoo()
'hello, zoo'

在您的cpp文件中,您需要将类方法的定义放在适当的名称空间中,而不是在using namespace ...声明中.例如,对于zoo.cpp,将为:

In your cpp files you need to put definitions of the class methods inside the appropriate namespaces instead of using namespace ... declarations. For example for zoo.cpp that will be:

namespace extzoo
{
namespace intzoo
{
const std::string zoo::hello_zoo() {
// ... code
}

const std::string zoo::getname_zoo() {
// ... code
}
}
}

------------------------------------------------- 更新 -------------------------------------------- ----------------------------------

--------------------------------------------- Update ------------------------------------------------------------------------------

使用{}在c ++代码中添加作用域以分离python作用域的定义:

Add scope in c++ code to separate definitions of python scope using {}:

BOOST_PYTHON_MODULE(pyintf) { // set scope to pyintf

{
    scope intzoo
    = class_<DummyZoo>("intzoo"); // define and set scope to pyintf.intzoo

    class_<extzoo::intzoo::zoo>("zoo")
    // Expose the function hello_zoo().
       .def("hello_zoo", &extzoo::intzoo::zoo::hello_zoo)
        // Expose the function getname_zoo().    
        .def("getname_zoo", &extzoo::intzoo::zoo::getname_zoo)
        ;
} // revert scope to pyintf


{
    scope intanim
        = class_<DummyAnimal>("intanim"); // define and set scope to pyintf.intanim

    class_<extanim::intanim::animal>("animal")
        // Expose the function hello_animal().
        .def("hello_animal", &extanim::intanim::animal::hello_animal)
        // Expose the function getname_animal().    
        .def("getname_animal", &extanim::intanim::animal::getname_animal)
        ;
} // revert scope to pyintf

}

测试:

>>> import pyintf
>>> dir(pyintf)
['__doc__', '__file__', '__name__', '__package__', 'intanim', 'intzoo']
>>> pyintf.intanim.animal().hello_animal()
'hello, animal'
>>> from pyintf import intanim
>>> from pyintf import extanim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name extanim
>>> from pyintf import intzoo
>>> 

请注意,由于未定义此类名称的范围,因此在导入extanim时会出现错误.

Note that error on importing extanim is expected as there isn't scope of such name defined.