CLion에서 ROS2 환경 셋팅
ROS2 setup tutorial in CLion
-
Prepare the ROS2 environment
-
Create a directory for a new workspace and navigate into it (e.g. ~/robot_ws)
-
Create an example package
cd ~/robot_ws && mkdir src && cd src ros2 pkg create --build-type ament_cmake my_project
-
Build a workspace in terminal and generate a compilation database
- Let’s assume that only selected packages are built.
cd .. colcon build --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON --symlink-install --packages-select my_project
-
When build is finished, make sure that robot_ws\build contains a compile_commands.json file.
-
In CLion, call File - Open from the main menu and select the compile_commands.json file in the top-level build directory
-
Call Tools - Compilation Database - Change Project Root from the main menu and select the workspace directory (robot_ws in our case).
How to build a package
1. Create a script for the ‘colcon build’ commands
-
Navigate to the build logs directory. In our case, it’s ~/robot_ws/log/latest_build my_project. Open the command.log file from the latest build.
-
Copy the commands into another file and modify them to the following:
#!/bin/zsh source /opt/ros/foxy/setup.zsh source ~/robot_ws/install/local_setup.zsh CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}:/opt/ros/foxy /usr/bin/cmake ~/robot_ws/build/my_project -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_INSTALL_PREFIX=~/robot_ws/install/my_project CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}:/opt/ros/foxy /usr/bin/cmake --build ~/robot_ws/build/my_project -- -j6 -l6 CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}:/opt/ros/foxy /usr/bin/cmake --install ~/robot_ws/build/my_project
-
Save the file as a .zsh script. In our example, it is called cmake_commands.zsh and placed into ~/robot_ws/build/my_project.
2. Create a custom build target
-
In CLion, go to Settings / Preferences - Build, Execution, Deployment - Custom Build Targets and click ‘+’ to add a new target.
-
Set each item.
-
Toolchain field: Set the value to default.
-
Build field: Click ‘…’ icon to add an external.
-
Program field: Select the newly created script(cmake_commands.zsh).
-
Set the Working directory to the package build directory.
-
-
-
Save the custom target.
3. Create a run/debug configuration for the custom build target
-
Go to Run - Edit Configurations, click ‘+’ and select Custom Build Application.
-
In the configuration settings, select the Target and make sure to remove Build from the Before launch area.
4. Build a package
-
Select the build configuration.
-
Build through the icon.
5. Run/debug a package
-
Open the Edit Configurations dialog again.
-
Modify the configuration for build to the following:
-
Executable field: select binary file or script file.
-
example: ~/robot_ws/build/my_project/src/run_debug_commands.zsh
#!/bin/zsh source /opt/ros/foxy/setup.zsh source ~/robot_ws/install/local_setup.zsh ~/robot_ws/build/manibot_bringup/joy_commander
-
-
option: Select the Run with Administrator privileges checkbox.
-
-
After saving the configuration, it is ready to be Run or Debugged.