python调用c++
cmake_minimum_required(VERSION 3.0.2)
project(drl_dwa)
## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
geometry_msgs
nav_msgs
std_msgs
sensor_msgs
)
# 寻找Boost库
find_package(Boost REQUIRED COMPONENTS python)
find_package(PythonLibs REQUIRED)
# 添加头文件路径
include_directories(
include
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIRS}
)
# 添加链接库路径
link_directories(
${catkin_LIBRARY_DIRS}
${Boost_LIBRARY_DIRS}
)
# 生成共享库
add_library(DWAPlanner SHARED src/DRL_dwa.cpp)
# 链接Boost和Python库
target_link_libraries(DWAPlanner ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
# 输出共享库文件
set_target_properties(DWAPlanner PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_LIB_DESTINATION}
)
# 设置编译选项
set_target_properties(DWAPlanner PROPERTIES PREFIX "")
set_target_properties(DWAPlanner PROPERTIES OUTPUT_NAME "DDQN")
# 添加编译选项
add_definitions(-DBOOST_PYTHON_STATIC_LIB)
# 添加链接选项
if(WIN32)
set_target_properties(DWAPlanner PROPERTIES SUFFIX ".pyd")
else()
set_target_properties(DWAPlanner PROPERTIES SUFFIX ".so")
endif()
# 安装共享库
install(TARGETS DWAPlanner
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)
set_target_properties(DWAPlanner PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/scripts
LIBRARY_OUTPUT_NAME DWAPlanner
)
c++调用python
cmake_minimum_required(VERSION 3.0.2)
project(drl_dwa)
## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)
# set(PYTHON_EXECUTABLE "/home/xj/anaconda3/envs/RL/bin/python3")
# set(PYTHON_PATH "/home/xj/DRL_DWA_git/DWA_ws/src/drl_dwa/scripts")
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
geometry_msgs
nav_msgs
std_msgs
sensor_msgs
)
# Set the path to Boost
set(Boost_INCLUDE_DIRS /usr/include)
set(Boost_LIBRARY_DIRS /usr/lib/x86_64-linux-gnu)
# Find Boost libraries
find_package(Boost REQUIRED COMPONENTS python)
find_package(PythonLibs REQUIRED)
# Add the include directories
include_directories(
include
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIRS}
)
# Add the linker directories
link_directories(
${catkin_LIBRARY_DIRS}
${Boost_LIBRARY_DIRS}
)
catkin_package(
CATKIN_DEPENDS roscpp rospy geometry_msgs nav_msgs sensor_msgs std_msgs
INCLUDE_DIRS include
)
# Add the Python bindings library
add_library(DRL_dwa SHARED src/DRL_dwa.cpp)
add_executable(DRL_dwa_node src/DRL_dwa.cpp)
target_link_libraries(DRL_dwa_node DRL_dwa ${catkin_LIBRARIES} ${Boost_LIBRARIES})
target_include_directories(DRL_dwa_node PRIVATE ${PYTHON_PATH})
# Install the Python bindings library
install(TARGETS DRL_dwa_node
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)
# Install the Python module
install(DIRECTORY src/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.py"
)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(DRL_dwa_node ${PYTHON_LIBRARIES})
find_library(BOOST_PYTHON_LIBRARIES NAMES boost_python38)
if(BOOST_PYTHON_LIBRARIES)
target_link_libraries(DRL_dwa_node ${BOOST_PYTHON_LIBRARIES})
endif()