Regarding Dependencies

Hi,

Following the training documents, I see when we create a new package we add dependencies like:

ros2 pkg create --build-type ament_python movement_pkg --dependencies rclpy custom_interfaces std_msgs geometry_msgs sensor_msgs

But I still use custom_interfaces, geometry_msgs without adding in dependencies.

So which way is right? adding or no add?

Thanks

Hi @NguyenDuyDuc ,

It is always best to add all the package dependencies either during creating of the package or after creating the package.

I have noticed at several times that in Python, if you declare a dependency or not, Python scripts somehow import them and use them. But if you try to do the same with a ament_cmake package with C++ scripts, then you will be forced to add the dependency package into CMakeLists.txt and package.xml.

So, by default, ament_cmake packages enforce you to add the dependencies. So as a good practice, it is always best to add all the dependencies to your ROS2 package, either during creation or post-creation of your package.

I hope this clarified your doubt. Always make it a practice to add all the dependencies to a package, irrespective of whatever type of package you are creating - that is either ament_python or ament_cmake.

Regards,
Girish

1 Like

Hi @girishkumar.kannan

If I add later, which command will I add with packages in package.xml?

add dependencies as same as the command rclpy but change the package name?

Hi @NguyenDuyDuc ,

If you want to add dependencies later you must add the exact package name of the dependency into package.xml.

For example, your are adding custom_interfaces package that you created, then you would add it into the package.xml with the following line:

<depend>custom_interfaces</depend>

There is no command that will add a dependency into your ROS2 package’s package.xml file. You have to do that manually by yourself, carefully and correctly!

Regards,
Girish

1 Like

Let me add here that adding during package creation will save you some time, effort and the occasional bugs that result from manually editing package.xml and CMakeLists.txt.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.