ROS C++ catkin not finding my custom messages

Courier picture Courier · Aug 3, 2016 · Viewed 7.1k times · Source

The compiler is telling that the messages (.../message.h) being not found. See my CMakeLists.txt below

cmake_minimum_required(VERSION 2.8.3)
project(my_package)
add_compile_options(-std=c++11)


find_package(catkin REQUIRED
  COMPONENTS cv_bridge
  image_transport
  roscpp
  #rospy
  sensor_msgs
  std_msgs
  message_generation
  genmsg
  external_package
  )
find_package(nodelet REQUIRED)


#----
add_message_files( FILES
  my_message1.msg
  my_message2.msg

  )

generate_messages( DEPENDENCIES
  std_msgs sensor_msgs
  )

catkin_package(
  CATKIN_DEPENDS  message_runtime std_msgs sensor_msgs roscpp cv_bridge image_transport
  )



#******     EXTRA PACKAGES
find_package(LAPACK REQUIRED)
find_package(BLAS REQUIRED)
find_package( PkgConfig REQUIRED)
FIND_PACKAGE(Boost)
find_package( OpenCV )
include_directories(${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} )

SET(MY_FLAG ok) # OR no!!

if(MY_FLAG)
#**** exe files
set(exefiles
  file1
  file2
  )

foreach(file ${exefiles})
  add_executable(${file} ${CMAKE_CURRENT_SOURCE_DIR}/pathToExeFiles/${file}.cpp ) 
endforeach(file)


#******  Lib & link
include_directories(${SRC}/pathToMyLib)
set(MY_LIB
  lib1
  lib2
  libn
  )
endif(MY_FLAG)

foreach(file ${exefiles})
  target_link_libraries(${file2link} 
    ${MY_LIB} 
    ${MY_LIB} 
    ${MY_LIB} 
    ${catkin_LIBRARIES}
    ${Boost_LIBRARIES}
    ${gsl_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES}
    ${OpenCV_LIBRARIES}
    )

  add_dependencies(${file} external_package_generate_messages_cpp  ${${my_package}_EXPORTED_TARGETS})
endforeach(file)


ADD_SUBDIRECTORY(src)

However, am able to compile with the following trick. I first set My_FLAG to flase and compile. Next, I set it back to true and compile again. By doing so it works fine. But... I could guess there should be a more elegant/professional/straightforward solution. Am so far not able to detect the cause of this problem. Any solution please?

I think the issue is related to the order of the dependencies... Which? No idea...

Answer

cassinaj picture cassinaj · Aug 3, 2016

It looks like you are not adding message generation dependency when adding executable targets within the loop

if(MY_FLAG)
#**** exe files
set(exefiles
  file1
  file2
  )

foreach(file ${exefiles})
  add_executable(${file} ${CMAKE_CURRENT_SOURCE_DIR}/pathToExeFiles/${file}.cpp ) 
  add_dependencies(${file} external_package_generate_messages_cpp  ${${my_package}_EXPORTED_TARGETS})
endforeach(file)
....

What is most likely happening in your case is, the first run with MY_FLAG=Off you generate the messages in the second foreach where you actually add the dependencies. The second run with MY_FLAG=On works because now the messages have been already generated.