Reputation: 1809
I have an example with the following structure.
├── 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"); }
And now upon running
╰─ ./notworking
[1] 14879 segmentation fault (core dumped) ./notworking
What am I missing to get this module to load properly here? I have scoured the documentation, in particular the build systems section, but have come up empty.
It also seems to be the case for me when using other wrappers in pybind11 from C++.
Upvotes: 1
Views: 5788
Reputation: 2847
I've got a modified version of your example to run with both local and native modules. Basic procedure was as follows:
Install python3.6, python3.6-dev and CMake (3.10 latest). I only have python3.6 installed (version 3.6.3)
Download pybind11-master from github and unzip. In the unzipped folder:
mkdir build
cd build
cmake ..
make check -j 4
sudo make install
Create the "notworking" project with simple main.cpp and calc.py sources:
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 (this must be present in the same folder:)
def add(i, j):
return i + j
My simple CMakeLists.txt file:
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 and running gives the output:
Hello, World!
3
Hope this is of some help.
Upvotes: 5