2017-12-11 5 views
0

Ich habe ein Beispiel mit der folgenden Struktur.Segfault bei Verwendung von pybind11 Wrappern 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"); } 

Und jetzt beim Fahren

╰─ ./notworking 
[1] 14879 segmentation fault (core dumped) ./notworking 

Was fehlt mir, damit dieses Modul hier richtig geladen wird? Ich habe die documentation, insbesondere die Build Systems Sektion, abgesucht, bin aber leer ausgegangen.

Es scheint auch der Fall zu sein, wenn Sie andere Wrapper in pybind11 von C++ verwenden.

Antwort

2

Ich habe eine modifizierte Version Ihres Beispiels für die Ausführung mit lokalen und nativen Modulen. Das grundlegende Verfahren war wie folgt:

Installieren Sie python3.6, python3.6-dev und CMake (3.10 neuesten). Ich habe nur Python3.6 installiert (Version 3.6.3)

Laden Sie pybind11-Master von github und entpacken. Im entpackten Ordner:

mkdir build 
cd build 
cmake .. 
make check -j 4 
sudo make install 

Erstellen Sie das "notworking" Projekt mit einfachen main.cpp und calc.py Quellen:

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 (muss im selben Ordner vorhanden sein :)

def add(i, j): 
    return i + j 

Meine einfache CMakeLists.txt Datei:

cmake_minimum_required(VERSION 3.5) 
project(notworking) 

find_package(pybind11 REQUIRED) 

add_executable(notworking main.cpp) 
target_link_libraries(notworking PRIVATE pybind11::embed) 

Bau und Betrieb gibt die Ausgabe:

Hello, World! 
3 

Hope this eine Hilfe ist.