pybind11
pybind11 copied to clipboard
How to integrate external library like ACADO Toolkit with pybind11[QUESTION]
I was able to build the library file with the ACADO header files but when I try to import the library it throws segmentation fault. When I checked what function inside my cpp code was causing segfault, I came to know that ACADO library specific functions were causing segfault.
My CMakeLists.txt file: `cmake_minimum_required(VERSION 3.0) project(ur_mpc)
Generate compile_commands.json for clang tools
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CATKIN_PACKAGE_DEPENDENCIES pybind11_catkin )
Pybind11 configuration must occur before find_package(catkin)
option(USE_PYBIND_PYTHON_3 "Use python3-compatible python bindings" OFF) if(USE_PYBIND_PYTHON_3) set(PYTHON_EXECUTABLE /usr/bin/python3) set(PYBIND11_PYTHON_VERSION 3.6 CACHE STRING "") endif(USE_PYBIND_PYTHON_3)
find_package(catkin REQUIRED COMPONENTS roslib ${CATKIN_PACKAGE_DEPENDENCIES} )
find_package(Boost REQUIRED COMPONENTS system filesystem )
find_package(PCL 1.7 REQUIRED)
find_package(OpenCV REQUIRED) find_package(Eigen3 3.3 REQUIRED NO_MODULE) ###################################
catkin specific configuration
###################################
catkin_package( INCLUDE_DIRS include ${EIGEN3_INCLUDE_DIRS} CATKIN_DEPENDS ${CATKIN_PACKAGE_DEPENDENCIES} LIBRARIES ${PROJECT_NAME} DEPENDS Boost )
###########
Build
###########
include_directories( include ${EIGEN3_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS} )
find_package(catkin_simple REQUIRED)
catkin_simple()
ARM NEON flags
if("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "armv7l") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv7-a -mfpu=neon -mfloat-abi=hard -funsafe-math-optimizations") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a -mfpu=neon -mfloat-abi=hard -funsafe-math-optimizations") message("enabling ARM neon optimizations") endif()
flags for speed (should already be enabled by default)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fopenmp -O3")
cs_add_library(ur_mpc_solver_leftarm externals/qpoases/SRC/Bounds.cpp externals/qpoases/SRC/Constraints.cpp externals/qpoases/SRC/CyclingManager.cpp externals/qpoases/SRC/Indexlist.cpp externals/qpoases/SRC/MessageHandling.cpp externals/qpoases/SRC/QProblem.cpp externals/qpoases/SRC/QProblemB.cpp externals/qpoases/SRC/SubjectTo.cpp externals/qpoases/SRC/Utils.cpp externals/qpoases/SRC/EXTRAS/SolutionAnalysis.cpp model/ur_robot_leftarm_mpc_export/acado_qpoases_interface.cpp model/ur_robot_leftarm_mpc_export/acado_integrator.c model/ur_robot_leftarm_mpc_export/acado_solver.c model/ur_robot_leftarm_mpc_export/acado_auxiliary_functions.c)
target_include_directories(ur_mpc_solver_leftarm PUBLIC model/ur_robot_leftarm_mpc_export/ externals/qpoases externals/qpoases/INCLUDE externals/qpoases/SRC)
Build python bindings
pybind11_add_module(UR_BIND SHARED src/ur_mpc_test.cpp ) add_dependencies(UR_BIND ur_mpc_solver_leftarm ${catkin_EXPORTED_TARGETS} ) target_link_libraries(UR_BIND PRIVATE ur_mpc_solver_leftarm /home/utsav/catkin_ws/src/ur_mpc/include ${catkin_LIBRARIES} )
if(USE_PYBIND_PYTHON_3)
unfortunately this appears not to be discoverable yet because the sourcing process assumes python2.7
set_target_properties(UR_BIND PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/lib/python3.6/dist-packages/${PROJECT_NAME} ) else(USE_PYBIND_PYTHON_3) set_target_properties(UR_BIND PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_PYTHON_DESTINATION} ) endif(USE_PYBIND_PYTHON_3)
TODO handle python2/3 case properly
install(TARGETS UR_BIND ARCHIVE DESTINATION ${CATKIN_PACKAGE_PYTHON_DESTINATION} LIBRARY DESTINATION ${CATKIN_PACKAGE_PYTHON_DESTINATION} ) cs_install() cs_export() `
It appears that any function defined in the header files of ACADO generated headers causes segfault. If I explicitly add that function in my main cpp file then segfault doesn't occur. I have very basic knowledge of cmake please let me know what is the issue here.
cmake_minimum_required(VERSION 3.0)
project(ur_mpc)
# Generate compile_commands.json for clang tools
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Make sure pybind11 is set up before finding catkin
find_package(pybind11 REQUIRED)
# Catkin configuration
find_package(catkin REQUIRED COMPONENTS
roslib
pybind11_catkin
Boost
PCL
OpenCV
Eigen3
)
# Set up catkin package dependencies
catkin_package(
INCLUDE_DIRS include ${EIGEN3_INCLUDE_DIRS}
CATKIN_DEPENDS ${CATKIN_PACKAGE_DEPENDENCIES}
LIBRARIES ${PROJECT_NAME}
DEPENDS Boost
)
# Include directories
include_directories(
include
${EIGEN3_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${catkin_INCLUDE_DIRS}
)
# Check if ARM NEON flags should be enabled
if("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv7-a -mfpu=neon -mfloat-abi=hard -funsafe-math-optimizations")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a -mfpu=neon -mfloat-abi=hard -funsafe-math-optimizations")
message("enabling ARM neon optimizations")
endif()
# General flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fopenmp -O3")
# Add QP Oases external library
cs_add_library(ur_mpc_solver_leftarm
externals/qpoases/SRC/Bounds.cpp
externals/qpoases/SRC/Constraints.cpp
externals/qpoases/SRC/CyclingManager.cpp
externals/qpoases/SRC/Indexlist.cpp
externals/qpoases/SRC/MessageHandling.cpp
externals/qpoases/SRC/QProblem.cpp
externals/qpoases/SRC/QProblemB.cpp
externals/qpoases/SRC/SubjectTo.cpp
externals/qpoases/SRC/Utils.cpp
externals/qpoases/SRC/EXTRAS/SolutionAnalysis.cpp
model/ur_robot_leftarm_mpc_export/acado_qpoases_interface.cpp
model/ur_robot_leftarm_mpc_export/acado_integrator.c
model/ur_robot_leftarm_mpc_export/acado_solver.c
model/ur_robot_leftarm_mpc_export/acado_auxiliary_functions.c
)
# Include paths for QP Oases
target_include_directories(ur_mpc_solver_leftarm PUBLIC
model/ur_robot_leftarm_mpc_export/
externals/qpoases
externals/qpoases/INCLUDE
externals/qpoases/SRC
)
# Build the Python bindings using pybind11
pybind11_add_module(UR_BIND SHARED src/ur_mpc_test.cpp)
# Make sure the UR_BIND target depends on ur_mpc_solver_leftarm
add_dependencies(UR_BIND ur_mpc_solver_leftarm ${catkin_EXPORTED_TARGETS})
# Link the necessary libraries for the Python bindings
target_link_libraries(UR_BIND PRIVATE
ur_mpc_solver_leftarm
${catkin_LIBRARIES}
/home/utsav/catkin_ws/src/ur_mpc/include
)
# Set target properties for the output library
if(USE_PYBIND_PYTHON_3)
set_target_properties(UR_BIND
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/lib/python3.6/dist-packages/${PROJECT_NAME}
)
else()
set_target_properties(UR_BIND
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_PYTHON_DESTINATION}
)
endif()