栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python调用C++

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python调用C++

1、Boost.Python方式

最靠谱的一种方式

这种情况下CMakeLists.txt 的编写:

cmake_minimum_required(VERSION 3.5)

# Find python and Boost - both are required dependencies
find_package(PythonLibs 2.7 REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)

# Without this, any build libraries automatically have names "lib{x}.so"
set(CMAKE_SHARED_MODULE_PREFIX "")

# Add a shared module - modules are intended to be imported at runtime.
# - This is where you add the source files
add_library(hello_ext MODULE hello_ext.cpp)

# Set up the libraries and header search paths for this target
target_link_libraries(hello_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(hello_ext PRIVATE ${PYTHON_INCLUDE_DIRS})

代码段的编写:

BOOST_PYTHON_MODULE( CL_CBS ) {
  using namespace boost::python;

  class_ >  ("v1_double")
  .def(vector_indexing_suite > ());

  class_ > >  ("v2_double")
  .def(vector_indexing_suite > > ());
   //此处,python端需要的是三重vector的数据结构,这里也要把单vector,二重vector都写上,如上
  class_ > > > ("v3_double")
  .def(vector_indexing_suite > > >());
  // 一般函数
  def( "main_o", main_o );
  
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1019238.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号