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

[ros2] components registration from subdirectory CMakeLists file

asked 2020-09-10 07:09:31 -0500

yardas gravatar image

I have a package that is separated into couple of subdirectories, each has its own CMakeLists.txt file that is included to the main CMakeLists.txt file by using add_subdirectory(). Each of these subdirectories contains the source code files for a different component that I want to use: e.g, I have a detection subdirectory, a tracking subdirectory and so forth. I am trying to register node components from within the subdirectories CMakeLists files by using rclcpp_components_register_nodes() but it seems as if the nodes don't get registered when I try to load them. Furthermore, they are not located at share/ament_index/resource_index/rclcpp_components/ like other components that I use.

If I take the contents of the subdirectories' CMakeLists.txt file to the main CMakeLists.txt and remove the call to add_subdirectory, everything works fine.

Anyone done something similar before?

Thanks in advance for any help.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-09-11 15:46:54 -0500

sloretz gravatar image

If I take the contents of the subdirectories' CMakeLists.txt file to the main CMakeLists.txt and remove the call to add_subdirectory, everything works fine.

This is probably because in CMake each call to add_subdirectory() creates a new scope, meaning CMake variables set in the subdirectory are not visible to the parent directory.

It looks like rclcpp_components_register_nodes()calls_rclcpp_components_register_package_hook This is a macro that calls ament_register_extension(). This appends to the CMake variable AMENT_EXTENSIONS_ament_package, presumably to register something to be called when ament_package() is called. Since this is a subdirectory, the call to ament_package() in the parent directory doesn't see the changed value.

You might be able to work around this setting that variable in the PARENT_SCOPE at the very end of the <subdirectory>/CMakeLists.txt, though, it's possible other variables used by rclcpp_components_register_nodes() will need to be set in the parent scope too.

set(AMENT_EXTENSIONS_ament_package ${AMENT_EXTENSIONS_ament_package} PARENT_SCOPE)

Alternatively, when a CMake project uses add_subdirectory() it might be a sign that it has gotten too big and should be split into smaller projects.

edit flag offensive delete link more

Comments

Thank you for your response, yes what you're saying makes sense. Currently the project is still quite small and well organized so I think that I'll just remove the add_subdirectory() and do everything on the main cmakelists file as you suggested. Btw, do you know of any cmakelists style guide or something similar?

Thank you again :)

yardas gravatar image yardas  ( 2020-09-13 02:18:56 -0500 )edit

I'm afraid I don't know of any CMake style guides :( , though there is a package for linting CMake files: https://github.com/ament/ament_lint/t... which seems to use a fork of https://github.com/richq/cmake-lint/

sloretz gravatar image sloretz  ( 2020-09-14 11:43:49 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-09-10 07:09:31 -0500

Seen: 1,046 times

Last updated: Sep 11 '20