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

Errors in creating Python interface for multiple C++ files in a shared library

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

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

注意:为使问题简单起见,我没有使用过类,否则我可以轻松地将"动物"和"动物"分类。

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

任何指导将不胜感激。

谢谢,

动物

+++++++++++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * 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解释器命令和输出:

import zoo

zoo.getname_zoo()


请输入您最喜欢的动物园名称: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 +++++++++++++++

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

1
2
3
4
5
6
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"模块时,出现以下错误。

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


谁能告诉我我做的不正确吗?

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


通过将我想公开的所有方法都放在一个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的更正。