将ros cmake添加到已存在的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名称,并将find_package、include_directions和catkin_package添加到cmake中,以以下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()'

知道我该怎么解决吗?我一无所知。

附言:我有另一个使用python-ros的工作区,它发布/订阅没有问题,在我在cpp中添加ros部分之前,这段代码运行得很好。

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

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

最新更新