ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How do I generate URDFs at compile time?

asked 2017-02-17 12:25:45 -0600

VictorLamoine gravatar image

updated 2017-02-17 12:29:55 -0600

I sometime use robots definitions containing many links and generating the URDFs from my XACROs files takes more than 5 seconds. This slows down my application startup because everything is waiting for XACRO to finish it's job.

Instead of generating the URDF with XACRO at run-time (in the launch file) I would like to generate them at compile time, ideally only when needed (= a modification has been done).

How can I do that?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-02-17 12:28:07 -0600

VictorLamoine gravatar image

updated 2017-02-22 16:13:04 -0600


As Gijs pointed out, there is already a CMake macro for that:

The macro is here:

It is possible to generate URDF files at compile time thanks to the add_custom_command and add_custom_target CMake commands.

In short

  • Create a custom command that generates URDF files from XACRO files
  • Create a target depending on all your URDF files and that depends on every target of you CMake script


  xacro --inorder robot.xacro -o robot.urdf
  "Generating ${PROJECT_NAME} robot URDF"

This creates a command that will generate your URDF file, using DEPENDS makes sure that everytime one of the XACRO file is changed the URDF will be generated. This means that URDFs are generated only if necessary as long as you list all dependencies correctly. You can multiply this command block to generate multiple URDF files.


This command creates a CMake target that is built when "make all" is invoked and that depends on the URDF files. This ensures that the custom command will be called. You should list here all your generated URDF files.

In your launch file instead of something like:

<param name="robot_description" command="$(find xacro)/xacro --inorder $(find xacro_urdf_cmake)/urdf/robot.xacro" />

You now should use:

<param name="robot_description" textfile="$(find xacro_urdf_cmake)/urdf/robot.urdf" />

Instead of generating the URDF at run-time (in the launch file), you can now use the URDF generated at compile time.


  • It does not work if your xacro files have ($find xacro_urdf_cmake) because xacro is invoked by CMake and does not source the workspace you are working in. Use relative paths!
  • If you are writing some tests, you must manually specify that your test depends on the ${PROJECT_NAME}_URDFs_files target
  • URDFs are generated in the source package, you should ignore them with a .gitignore file

A demonstration package is available at xacro_urdf_cmake. Checkout the pipeline to see what is does!

edit flag offensive delete link more



This does look a bit like a 'duplicate' of wiki/xacro - Building from CMakeLists.txt?

gvdhoorn gravatar image gvdhoorn  ( 2017-02-18 04:08:21 -0600 )edit

Oh yes it is! I wish I had found this while searching. I'll improve the macro with a PR is needed. Thanks for the link

VictorLamoine gravatar image VictorLamoine  ( 2017-02-22 16:05:39 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-02-17 12:25:45 -0600

Seen: 718 times

Last updated: Feb 22 '17