【问题标题】:Add ros cmake to an already existing cmake将 ros cmake 添加到已经存在的 cmake
【发布时间】:2022-01-03 21:40:27
【问题描述】:

我有一个非常大的代码,这个 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>

但我不断收到此错误,根据谷歌的说法,这意味着我犯了 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 的工作区,可以毫无问题地发布/订阅,并且在我在 cpp 中添加 ros 部分之前,这段代码运行良好。

【问题讨论】:

    标签: c++ cmake ros


    【解决方案1】:

    确保您还链接到 catkin 指定的库 (${catkin_LIBRARIES}),因为 ROS 库列在:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      相关资源
      最近更新 更多