2017-12-11 29 views
0

다음 구조의 예제가 있습니다.Segfault는 C++에서 pybind11 래퍼를 사용할 때

├── 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 

이 모듈을 올바르게로드하려면 무엇이 손실됩니까? 나는 , 특히 빌드 시스템 섹션을 샅샅이 조사했지만 비어 있습니다.

C++의 pybind11에 다른 래퍼를 사용하는 경우에도 나에게 맞는 것 같습니다.

답변

2

로컬 및 네이티브 모듈 모두를 실행하기 위해 수정 된 버전의 예제가 있습니다. 기본 절차는 다음과 같습니다 :

python3.6, python3.6-dev 및 CMake (3.10 최신)를 설치하십시오. Python3.6 만 설치 (버전 3.6.3)

github에서 pybind11-master를 다운로드하고 압축을 풉니 다. 이 수행해야합니다 (

#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 :

하여 Main.cpp :

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

간단 MAIN.CPP와 calc.py 소스와 함께 "notworking"프로젝트를 만듭니다 : 압축을 푼 폴더에서

: 같은 폴더 :

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

내 간단한 CMakeLists.txt 파일에 존재 이 도움이다

Hello, World! 
3 

희망 : 17,451,515,

cmake_minimum_required(VERSION 3.5) 
project(notworking) 

find_package(pybind11 REQUIRED) 

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

빌드 및 실행의 출력을 제공합니다.