为共享库中的多个 C++ 文件创建 Python 接口时出错
Errors in creating Python interface for multiple C++ files in a shared library
我有两个 C++ 文件,分别命名为 "animal.cpp" 和 "zoo.cpp"。这些文件被编译为共享库"zoo.so"。我想从 Python 解释器访问这些文件中定义的函数。
但是,我遇到了错误。我尝试进行一些更改,但遇到了不同类型的错误。希望Boost-Python高手指导解决这些问题。
注:为简单起见,我没有使用类,否则我很容易将类变为"animal"和"zoo"。
我想从 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 解释器命令和输出:
import zoo
zoo.getname_zoo()
请输入您喜欢的动物园名称:zoo
'Your favorit zoo name is: zoo'
zoo.getname_animal()
回溯(最后一次调用):
文件“”,第 1 行,位于
AttributeError: 'module' 对象没有属性 'getname_animal'
当我在 animal.cpp 文件中进行以下更改时,出现不同的错误。
BOOST_PYTHON_MODULE(动物园){
而不是
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 个退出状态
++++++++++++++++++++++++++++更新++++更新+++++更新+++++++++++++++
考虑到 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" 模块时出现以下错误。
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
感谢丹的更正。
我有两个 C++ 文件,分别命名为 "animal.cpp" 和 "zoo.cpp"。这些文件被编译为共享库"zoo.so"。我想从 Python 解释器访问这些文件中定义的函数。
但是,我遇到了错误。我尝试进行一些更改,但遇到了不同类型的错误。希望Boost-Python高手指导解决这些问题。
注:为简单起见,我没有使用类,否则我很容易将类变为"animal"和"zoo"。
我想从 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 解释器命令和输出:
import zoo
zoo.getname_zoo()
请输入您喜欢的动物园名称:zoo
'Your favorit zoo name is: zoo'
zoo.getname_animal()
回溯(最后一次调用): 文件“”,第 1 行,位于 AttributeError: 'module' 对象没有属性 'getname_animal'
当我在 animal.cpp 文件中进行以下更改时,出现不同的错误。
BOOST_PYTHON_MODULE(动物园){
而不是
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 个退出状态
++++++++++++++++++++++++++++更新++++更新+++++更新+++++++++++++++
考虑到 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" 模块时出现以下错误。
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
感谢丹的更正。