I have a shared library hello_ext.so
. I need to import this shared library in a python script.
The directory is:
- project
| - py_binding
| - hello_ext.cpp
| - CMakeLists.txt
| - python_script.py
| - CMakeLists.txt
The binding is correct and works.
After cmake ..
and make
, the library can be called from the build
folder.
$ python3
$ from hello_ext import greet
$ greet()
'hello, world'
Therefore the pipeline works.
However, if I run the python script using python3 /<path_to_sscript>/python_script.py
I get this error:
Traceback (most recent call last):
File "/home/sharad/git_repository/longitudinal_control_pid/py_bindings/arduino_connection_usb.py", line 1, in <module>
from hello_ext import greet
ModuleNotFoundError: No module named 'hello_ext'
I believe the .so
is not visible from python script.
I am using cmake
as for the project.
CMakeLists.txt
under py_binding
dir:
cmake_minimum_required(VERSION 3.0.0)
set(ThisBinding hello_ext)
find_package(PythonLibs 3.6 REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)
set(CMAKE_SHARED_MODULE_PREFIX "")
add_library(${ThisBinding} MODULE hello_ext.cpp)
target_link_libraries(${ThisBinding} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(${ThisBinding} PRIVATE ${PYTHON_INCLUDE_DIRS})
CMakeLists.txt in the major directory:
cmake_minimum_required(VERSION 3.0.0)
# set the project name
project(longitudinal_control_pid CXX)
# Set some global variables
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
enable_testing()
add_subdirectory(googletest)
file(GLOB SOURCES
src/*.cpp
)
file(GLOB HEADERS
include/*.h
)
add_library(longitudinal_control_pid ${SOURCES})
target_include_directories(longitudinal_control_pid PUBLIC ./)
add_subdirectory(test)
add_subdirectory(py_bindings)
I am using Ubuntu 20.04
, python3
.
I want to make this script import the built shared library and use the bindings.
I have googled this and have not found a solution which worked for me.
Does anyone know how to make it work?
Thanks