将 ros cmake 添加到已经存在的 cmake

Add ros cmake to an already existing cmake

我有一个非常大的 cmake 代码可以运行:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(MYPROJECT)

set (CMAKE_CXX_STANDARD 11)

find_package(PCL 1.7 REQUIRED)
if(DEFINED PCL_LIBRARIES)
    list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4")
endif()
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable (main src/main.cpp)

target_link_libraries (main ${PCL_LIBRARIES})

现在,我想把这段代码的结果发布到一个ros主题上。所以我将这段代码添加到一个ros工作区,并且需要将ros的东西添加到cmake中。我将项目名称更改为 pkg 名称,并在 cmake 中添加了 find_package、include_directories 和 catkin_package,并以此 CMake 结尾:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(plc)

set (CMAKE_CXX_STANDARD 11)

find_package(PCL 1.7 REQUIRED)
if(DEFINED PCL_LIBRARIES)
    list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4")
endif()
find_package(catkin REQUIRED COMPONENTS
  pcl_conversions
  pcl_ros
  roscpp
)
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)

include_directories(${catkin_INCLUDE_DIRS})
include_directories(${PCL_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

catkin_package(CATKIN_DEPENDS roscpp pcl_ros pcl_conversions)

add_executable (main src/main.cpp)

target_link_libraries (main ${PCL_LIBRARIES})

还将此添加到 package.xml:

  <build_depend>pcl_conversions</build_depend>
  <build_depend>pcl_ros</build_depend>
  <build_depend>roscpp</build_depend>
  <build_export_depend>pcl_conversions</build_export_depend>
  <build_export_depend>pcl_ros</build_export_depend>
  <build_export_depend>roscpp</build_export_depend>
  <exec_depend>pcl_conversions</exec_depend>
  <exec_depend>pcl_ros</exec_depend>
  <exec_depend>roscpp</exec_depend>

但我一直收到这个错误,根据 google 的说法,这意味着我弄错了 CMake。

usr/bin/ld: main.cpp:(.text+0x6bdc): undefined reference to `ros::Rate::Rate(double)'
/usr/bin/ld: main.cpp:(.text+0x6beb): undefined reference to `ros::NodeHandle::ok() const'
/usr/bin/ld: main.cpp:(.text+0x6c07): undefined reference to `ros::Time::now()'
/usr/bin/ld: main.cpp:(.text+0x6c3e): undefined reference to `ros::spinOnce()'
/usr/bin/ld: main.cpp:(.text+0x6c4d): undefined reference to `ros::Rate::sleep()'
/usr/bin/ld: main.cpp:(.text+0x6c5e): undefined reference to `ros::Publisher::~Publisher()'
/usr/bin/ld: main.cpp:(.text+0x6c6d): undefined reference to `ros::NodeHandle::~NodeHandle()'
/usr/bin/ld: main.cpp:(.text+0x6cfc): undefined reference to `ros::Publisher::~Publisher()'
/usr/bin/ld: main.cpp:(.text+0x6d0b): undefined reference to `ros::NodeHandle::~NodeHandle()'

知道如何解决这个问题吗?我一头雾水

PS:我有另一个带有 python ros 的工作区 publishes/subscribes 没有问题,并且在我将 ros 部分添加到 cpp 之前这段代码运行良好。

确保你也link再次使用catkin指定的库(${catkin_LIBRARIES}),因为ROS库列在:

target_link_libraries (main
  ${PCL_LIBRARIES})
  ${catkin_LIBRARIES}
)