在 C++ 中使用 pybind11 包装器时出现段错误
Segfault when using pybind11 wrappers in C++
我有一个具有以下结构的示例。
├── CMakeLists.txt
├── ext
│ └── pybind11
└── main.cpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(notworking)
add_subdirectory(ext/pybind11)
add_executable(notworking
main.cpp)
target_link_libraries(notworking PRIVATE python3.6m)
target_link_libraries(notworking PRIVATE pybind11)
main.cpp
#include <pybind11/pybind11.h>
namespace py = pybind11;
int main() { py::object decimal = py::module::import("decimal"); }
现在 运行
╰─ ./notworking
[1] 14879 segmentation fault (core dumped) ./notworking
我错过了什么才能让这个模块在这里正确加载?我已经搜索了 documentation,尤其是构建系统部分,但结果是空的。
在 C++ 的 pybind11 中使用其他包装器时,我似乎也是这种情况。
我已经将您的示例修改为 运行,同时包含本地和本机模块。基本流程如下:
安装python3.6、python3.6-dev 和CMake(最新3.10)。我只安装了 python3.6(版本 3.6.3)
从 github 下载 pybind11-master 并解压。在解压缩的文件夹中:
mkdir build
cd build
cmake ..
make check -j 4
sudo make install
使用简单的 main.cpp 和 calc.py 来源创建 "notworking" 项目:
Main.cpp:
#include <pybind11/embed.h>
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{};
py::print("Hello, World!");
py::module decimal = py::module::import("decimal");
py::module calc = py::module::import("calc");
py::object result = calc.attr("add")(1, 2);
int n = result.cast<int>();
assert(n == 3);
py::print(n);
}
Calc.py(这必须存在于同一文件夹中:)
def add(i, j):
return i + j
我的简单CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.5)
project(notworking)
find_package(pybind11 REQUIRED)
add_executable(notworking main.cpp)
target_link_libraries(notworking PRIVATE pybind11::embed)
Building 和 运行ning 给出输出:
Hello, World!
3
希望对您有所帮助。
我有一个具有以下结构的示例。
├── CMakeLists.txt
├── ext
│ └── pybind11
└── main.cpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(notworking)
add_subdirectory(ext/pybind11)
add_executable(notworking
main.cpp)
target_link_libraries(notworking PRIVATE python3.6m)
target_link_libraries(notworking PRIVATE pybind11)
main.cpp
#include <pybind11/pybind11.h>
namespace py = pybind11;
int main() { py::object decimal = py::module::import("decimal"); }
现在 运行
╰─ ./notworking
[1] 14879 segmentation fault (core dumped) ./notworking
我错过了什么才能让这个模块在这里正确加载?我已经搜索了 documentation,尤其是构建系统部分,但结果是空的。
在 C++ 的 pybind11 中使用其他包装器时,我似乎也是这种情况。
我已经将您的示例修改为 运行,同时包含本地和本机模块。基本流程如下:
安装python3.6、python3.6-dev 和CMake(最新3.10)。我只安装了 python3.6(版本 3.6.3)
从 github 下载 pybind11-master 并解压。在解压缩的文件夹中:
mkdir build
cd build
cmake ..
make check -j 4
sudo make install
使用简单的 main.cpp 和 calc.py 来源创建 "notworking" 项目:
Main.cpp:
#include <pybind11/embed.h>
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{};
py::print("Hello, World!");
py::module decimal = py::module::import("decimal");
py::module calc = py::module::import("calc");
py::object result = calc.attr("add")(1, 2);
int n = result.cast<int>();
assert(n == 3);
py::print(n);
}
Calc.py(这必须存在于同一文件夹中:)
def add(i, j):
return i + j
我的简单CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.5)
project(notworking)
find_package(pybind11 REQUIRED)
add_executable(notworking main.cpp)
target_link_libraries(notworking PRIVATE pybind11::embed)
Building 和 运行ning 给出输出:
Hello, World!
3
希望对您有所帮助。