【发布时间】:2018-11-14 05:42:26
【问题描述】:
我正在尝试设置一个 CMake 项目,该项目在 Ubuntu 上使用 pybind11 为其 c++ 函数创建 python 绑定。
目录结构为:
pybind_test
arithmetic.cpp
arithmetic.h
bindings.h
CMakeLists.txt
main.cpp
pybind11 (github repo clone)
Repo contents (https://github.com/pybind/pybind11)
CMakeLists.txt 文件:
cmake_minimum_required(VERSION 3.10)
project(pybind_test)
set(CMAKE_CXX_STANDARD 17)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
include_directories(pybind11/include/pybind11)
add_executable(pybind_test main.cpp arithmetic.cpp)
add_subdirectory(pybind11)
pybind11_add_module(arithmetic arithmetic.cpp)
target_link_libraries(pybind_test ${PYTHON_LIBRARIES})
存储库构建成功并生成文件arithmetic.cpython-36m-x86_64-linux-gnu.so。如何将此共享对象文件导入 python?
pybind11 文档中的文档有这一行
$ c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
但我想使用 CMake 进行构建,而且我也不想在每次运行 python 来使用这个模块时都指定额外的包含目录。
我如何将这个共享对象文件像普通的 python 模块一样导入 python?
我使用的是 Ubuntu 16.04。
【问题讨论】:
标签: python c++ linux cmake pybind11