我有2个C ++类,我试图通过boost将其导出到python。下面是boost python模块:
#include "/path/to/header.h"
namespace bp = boost::python;
BOOST_PYTHON_MODULE(module){
bp::class_< metadata >( "metadata" )
.def_readwrite( "id", &metadata::id )
.def_readwrite( "status", &metadata::status );
bp::class_< device >( "device" )
.def(
"deviceConnect"
, (bool (*)( ::metadata,bool const ))( &::device::deviceConnect )
, ( bp::arg("meta"), bp::arg("verbose") ) )
.def(
"deviceStatus"
, (bool (*)( ::metadata,::std::string const,bool const ))( &::device::deviceStatus )
, ( bp::arg("meta"), bp::arg("id"), bp::arg("verbose") ) )
.staticmethod( "deviceConnect" )
.staticmethod( "deviceStatus" );
}
这两个类是设备和元数据,它们都在包含的标头中定义。我使用下面的cmakelists.txt构建代码,并输出一个我试图导入到python脚本中的SO文件。
project(device_pylib)
# Find default python libraries and interpreter
find_package (Python3 COMPONENTS Interpreter Development )
find_package(Boost 1.65.1 COMPONENTS python3 )
include_directories(${Boost_INCLUDE_DIR} ${Python3_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
# Build and link the pylib_auto module
add_library(device SHARED /path/to/boost/module.cpp)
target_link_libraries(device ${Boost_LIBRARIES} ${Python3_LIBRARIES})
add_dependencies(device Boost::python3)
# Tweaks the name of the library to match what Python expects
set_target_properties(device PROPERTIES SUFFIX .so)
set_target_properties(device PROPERTIES PREFIX "")
一切正常,并且SO文件正确构建。但是,当我像这样将其导入到我的python脚本中时:
from module import device
一旦我运行python脚本,它就会给我以下错误:
ImportError: /path/to/shared/object/module.so: undefined symbol: _ZN6device12deviceStatusE8metadataNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEb
我非常确定这与将对象从元数据类传递到设备类的成员函数有关。每当我将它们从函数原型中删除时,它就可以正常运行。在上述配置中我做错什么了吗?