Search code examples
c++cmakeros

Add ros cmake to an already existing cmake


I have a really big code with this cmake that works:

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})

Now, I want to publish the results of this code to a ros topic. So I added this code to a ros workspace, and need to add ros stuff into the cmake. I changed the project name to the pkg name, and added to the cmake the find_package, include_directories and catkin_package, ending with this 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})

also added this to the 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>

But I keep getting this errors, that according to google means that I made the CMake wrong.

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()'

Any idea how can I fix this? I'm clueless.

PS: I have another workspace with python ros that publishes/subscribes without problems, and this code was working perfectly before I added the ros part in cpp.


Solution

  • Make sure you also link agains the catkin specified libraries (${catkin_LIBRARIES}), because there the ROS libs are listed in:

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