Reputation: 1
I'm trying to make Python bindings for some C++ code that I created using an existing C++ library. I'm using PyBind11 and CMake to build the bindings and convert them into a shared library (.so) that I can import directly into my Python script. After creating the bindings and building the library, importing into my script throws a ModuleNotFoundError: No module named 'slangparser'
error.
My working directory looks like this:
toolsuite
--- CMakeLists.txt
--- build/debug/build
--- deps
--- slang
--- pybind11
--- src
--- pybind_wrapper.cpp
--- slangparser.cpp
I have configured my CMakeLists.txt file in the base directory to look as follows:
cmake_minimum_required(VERSION 3.6)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_PREFIX_PATH "/net/sw/python/3.7.4/bin")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
project(slang_toolsuite)
find_package (PythonLibs REQUIRED)
include_directories (${PYTHON_INCLUDE_DIRS})
add_subdirectory(deps/slang)
add_subdirectory(deps/pybind11)
pybind11_add_module(slangparser SHARED ${CMAKE_CURRENT_SOURCE_DIR}/src/pybindwrapper.cpp)
target_link_libraries(slangarser PUBLIC slang::slang)
The C++ code that I want to run is contained in slangparser.cpp
, the glue code that wraps this code into the bindings are present in pybindwrapper.cpp
which looks like this:
#include "pybind11/pybind11.h"
#include "slangparser.cpp"
namespace py = pybind11;
PYBIND11_MODULE(slangparser, m) {
m.def("slangParser", &slangParser, "C++ function");
}
I'm not sure where the problem is, the project is building in VSCode without any problems into the /build/debug/build
folder. I just can't seem to import the slangparser module in the generated .so library into my Python script. Could I get some help? Thanks!
Also, the generated .so library has file name with python3.6m, instead of python3.7 which I'm using. Could that be clue to the problem?
Upvotes: 0
Views: 664
Reputation: 13
I don't know if you are using pybind11 as a static library or not. If so, then the code snippet I am sending for CMakeLists.txt
is enough to call the wrapper.
cmake_minimum_required(VERSION 3.20)
project(Project LANGUAGES CXX C)
set(ENV{Path} PATH)
add_subdirectory("{pybind11Path}\\pybind11")
add_subdirectory("{srcPath}\\srcFolder")
pybind11_add_module(Project {srcPath}/src.cpp)
About PYBIND11_MODULE
You need to call it inside of the source file, which you want to be wrapped with Python, at least that works.
Next point is about Python distribution along with dev-tools which must be present in Environment Path, because pybind11 calls Python.h
without that file it's impossible to work with pybind11.
Finally I am sending a link of pybind11 tutorial. Please follow it, it's quite good according to my experience. Good Luck!!
Upvotes: 0