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?