ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

How to add a node to CMakeLists.txt to build the package?

asked 2015-07-14 05:17:51 -0500

Kishore Kumar gravatar image

updated 2022-01-22 16:16:31 -0500

Evgeny gravatar image

I have a node in a package and it is working fine, now i added one more node to src folder of same package, how can i add the node to CMakeLists.txt to compile and run the package. Is there any automatic way to build the package without modifying the CMakeLists.txt, just by inserting the node to src folder.


Edit: I have two nodes named image_converter.cpp and image_invert.cpp with same dependencies. My CMakeLists.txt is as follows. When i try to build it using cmake, i get the error as shown here. How to modify the CMakeLists.txt.

 cmake_minimum_required(VERSION 2.8.3)
  project(opencv_pkg)

  find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  sensor_msgs
  std_msgs
  )

   include_directories(
     ${catkin_INCLUDE_DIRS}
   )

  add_library($image_convert src/image_converter.cpp)
  target_link_libraries($image_convert
    ${avcodec_LIBRARIES}
   ${swscale_LIBRARIES}
   ${catkin_LIBRARIES}
    )

   add_executable($image_convert_node src/image_converter.cpp)
   target_link_libraries($image_convert_node
    $image_convert
     ${avcodec_LIBRARIES}
   ${swscale_LIBRARIES}
   ${catkin_LIBRARIES}
    )

    add_library($image_invert src/image_invert.cpp)
    target_link_libraries($image_invert
     ${avcodec_LIBRARIES}
    ${swscale_LIBRARIES}
    ${catkin_LIBRARIES}
     )

   add_executable($image_invert_node src/image_invert.cpp)
   target_link_libraries($image_invert_node
    $image_invert
     ${avcodec_LIBRARIES}
     ${swscale_LIBRARIES}
    ${catkin_LIBRARIES}
    )
edit retag flag offensive close merge delete

Comments

@Kishore Kumar: please do not use answers to post updates to your question. Use the edit button for that (it's right under the main text of your question, with the little pen).

gvdhoorn gravatar image gvdhoorn  ( 2015-07-14 08:12:21 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-07-14 05:37:04 -0500

mgruhler gravatar image

updated 2015-07-14 09:52:06 -0500

No, you have to specify this in the CMakeLists.txt manually. How should the compiler know which src and header files to compile together (besides magic. thanks for pointing this out in the comments ;-) )?


A problem I see in your CMakeLists.txt is the target name for your executables and libraries. They should not start with a $ sign. The dollar sign with curly brackets is for getting the values of variables, as it is done correctly below when linking against the ${catkin_LIBRARIES} (I'm not sure if the other two exist).

so it should be:

  add_library(image_convert src/image_converter.cpp)
  target_link_libraries(image_convert
    ${avcodec_LIBRARIES}
    ${swscale_LIBRARIES}
    ${catkin_LIBRARIES}
  )

  add_executable(image_convert_node src/image_converter.cpp)
  target_link_libraries(image_convert_node
    image_convert
    ${avcodec_LIBRARIES}
    ${swscale_LIBRARIES}
    ${catkin_LIBRARIES}
   )

   add_library(image_invert src/image_invert.cpp)
   target_link_libraries(image_invert
     ${avcodec_LIBRARIES}
     ${swscale_LIBRARIES}
     ${catkin_LIBRARIES}
   )

   add_executable(image_invert_node src/image_invert.cpp)
   target_link_libraries(image_invert_node
     image_invert
     ${avcodec_LIBRARIES}
     ${swscale_LIBRARIES}
     ${catkin_LIBRARIES}
   )
edit flag offensive delete link more

Comments

1

jep must be done by

add_executable(node_name [files])
target_link_libraries(node_name [custom libraries ... ] {catkin_LIBRARIES} )
cyborg-x1 gravatar image cyborg-x1  ( 2015-07-14 06:05:06 -0500 )edit
3

How should the compiler know which src and header files to compile together?

Magic? :)

gvdhoorn gravatar image gvdhoorn  ( 2015-07-14 08:12:47 -0500 )edit

sry i cant get your point exactly, can u tell me where and how should i edit to fix it

Kishore Kumar gravatar image Kishore Kumar  ( 2015-07-14 09:07:24 -0500 )edit

You need to add the lines inside your CMakeLists.txt There should be also some commented lines which should look like these, just comment them insert the cpp files for [files] and a nodename of your choice for node_name [custom libraries ...] are optional and the catkin_LIBRARIES you always need.

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-14 09:45:56 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-14 05:17:51 -0500

Seen: 3,960 times

Last updated: Jul 14 '15