vscode配置ROS2开发环境
我这里使用vscode的ssh功能开发ros2,记录一下如何配置补全。
安装插件
- C/C++
- C/C++ Extension Pack
- CMake
- CMake Tools
配置vscode补全文件
创建节点
我首先创建了我需要的功能包的模版
# --run--
ros2 pkg create --build-type ament_cmake hello_cpp_node --dependencies rclcpp
注意这里只是创建了一个模版,里面的CMakeLists.txt
也是一个模版的状态,我需要添加一些东西来补全它。
补全后的CMake如下:
cmake_minimum_required(VERSION 3.8)
project(hello_cpp_node)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
add_executable(hello_node src/hello_cpp_node.cpp)
ament_target_dependencies(hello_node rclcpp)
install(TARGETS
hello_node
DESTINATION lib/${PROJECT_NAME}
)
ament_package()
创建cmake文件
然后我回到工作空间的根目录,运行cmake命令:
# --run--
cd ~/ros2_ws
colcon build --packages-select hello_cpp_node --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
--cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
:告诉CMake生成compile_commands.json
--packages-select hello_cpp_node
:只构建选定的包,避免浪费时间
运行完之后把需要的文件拷贝到ros2_ws
文件夹,也可以使用软连接:
# 方法一
ln -s build/hello_cpp_node/compile_commands.json
# 方法二
cp build/hello_cpp_node/compile_commands.json .
配置.vsocde文件夹
接下来使用vscode的内置功能创建.vscode/c_cpp_properties.json
,该文件配置中添加一行
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/opt/ros/jazzy/include",
"${workspaceFolder}/install/**/include"
],
"defines": [],
"compileCommands": "${workspaceFolder}/compile_commands.json",
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
自动构建配置(可选)
可以添加.vscode/tasks.json
配置文件:
{
"version": "2.0.0",
"tasks": [
{
"label": "colcon build",
"type": "shell",
# 这里可以自己修改
"command": "colcon build --packages-select hello_cpp_node",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
最后重启vscode!
我配置好的目录树如下:
ros2_ws/
├── build
│ ├── COLCON_IGNORE
│ ├── compile_commands.json
│ └── hello_cpp_node
│ ├── ament_cmake_core
│ │ ├── hello_cpp_nodeConfig.cmake
│ │ ├── hello_cpp_nodeConfig-version.cmake
│ │ ├── package.cmake
│ │ └── stamps
│ │ ├── ament_prefix_path.sh.stamp
│ │ ├── nameConfig.cmake.in.stamp
│ │ ├── nameConfig-version.cmake.in.stamp
│ │ ├── package_xml_2_cmake.py.stamp
│ │ ├── package.xml.stamp
│ │ ├── path.sh.stamp
│ │ └── templates_2_cmake.py.stamp
│ ├── ament_cmake_environment_hooks
│ │ ├── ament_prefix_path.dsv
│ │ ├── local_setup.bash
│ │ ├── local_setup.dsv
│ │ ├── local_setup.sh
│ │ ├── local_setup.zsh
│ │ ├── package.dsv
│ │ └── path.dsv
│ ├── ament_cmake_index
│ │ └── share
│ │ └── ament_index
│ │ └── resource_index
│ │ ├── package_run_dependencies
│ │ │ └── hello_cpp_node
│ │ ├── packages
│ │ │ └── hello_cpp_node
│ │ └── parent_prefix_path
│ │ └── hello_cpp_node
│ ├── ament_cmake_package_templates
│ │ └── templates.cmake
│ ├── ament_cmake_uninstall_target
│ │ └── ament_cmake_uninstall_target.cmake
│ ├── ament_cppcheck
│ ├── AMENT_IGNORE
│ ├── ament_lint_cmake
│ ├── ament_uncrustify
│ ├── ament_xmllint
│ ├── cmake_args.last
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.28.3
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── a.out
│ │ │ │ ├── CMakeCCompilerId.c
│ │ │ │ └── tmp
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ └── tmp
│ │ ├── cmake.check_cache
│ │ ├── CMakeConfigureLog.yaml
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeRuleHashes.txt
│ │ ├── CMakeScratch
│ │ ├── hello_cpp_node_uninstall.dir
│ │ │ ├── build.make
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── compiler_depend.make
│ │ │ ├── compiler_depend.ts
│ │ │ ├── DependInfo.cmake
│ │ │ └── progress.make
│ │ ├── hello_node.dir
│ │ │ ├── build.make
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── compiler_depend.make
│ │ │ ├── compiler_depend.ts
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ ├── progress.make
│ │ │ └── src
│ │ │ ├── hello_cpp_node.cpp.o
│ │ │ └── hello_cpp_node.cpp.o.d
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── pkgRedirects
│ │ ├── progress.marks
│ │ ├── TargetDirectories.txt
│ │ └── uninstall.dir
│ │ ├── build.make
│ │ ├── cmake_clean.cmake
│ │ ├── compiler_depend.make
│ │ ├── compiler_depend.ts
│ │ ├── DependInfo.cmake
│ │ └── progress.make
│ ├── cmake_install.cmake
│ ├── colcon_build.rc
│ ├── colcon_command_prefix_build.sh
│ ├── colcon_command_prefix_build.sh.env
│ ├── compile_commands.json
│ ├── CTestConfiguration.ini
│ ├── CTestCustom.cmake
│ ├── CTestTestfile.cmake
│ ├── hello_node
│ ├── install_manifest.txt
│ └── Makefile
├── compile_commands.json
├── install
│ ├── COLCON_IGNORE
│ ├── hello_cpp_node
│ │ ├── lib
│ │ │ └── hello_cpp_node
│ │ │ └── hello_node
│ │ └── share
│ │ ├── ament_index
│ │ │ └── resource_index
│ │ │ ├── package_run_dependencies
│ │ │ │ └── hello_cpp_node
│ │ │ ├── packages
│ │ │ │ └── hello_cpp_node
│ │ │ └── parent_prefix_path
│ │ │ └── hello_cpp_node
│ │ ├── colcon-core
│ │ │ └── packages
│ │ │ └── hello_cpp_node
│ │ └── hello_cpp_node
│ │ ├── cmake
│ │ │ ├── hello_cpp_nodeConfig.cmake
│ │ │ └── hello_cpp_nodeConfig-version.cmake
│ │ ├── environment
│ │ │ ├── ament_prefix_path.dsv
│ │ │ ├── ament_prefix_path.sh
│ │ │ ├── path.dsv
│ │ │ └── path.sh
│ │ ├── hook
│ │ │ ├── cmake_prefix_path.dsv
│ │ │ └── cmake_prefix_path.sh
│ │ ├── local_setup.bash
│ │ ├── local_setup.dsv
│ │ ├── local_setup.sh
│ │ ├── local_setup.zsh
│ │ ├── package.bash
│ │ ├── package.dsv
│ │ ├── package.sh
│ │ ├── package.xml
│ │ └── package.zsh
│ ├── local_setup.bash
│ ├── local_setup.sh
│ ├── _local_setup_util_sh.py
│ ├── local_setup.zsh
│ ├── setup.bash
│ ├── setup.sh
│ └── setup.zsh
├── log
│ ├── build_2025-07-22_09-26-51
│ │ ├── events.log
│ │ ├── hello_cpp_node
│ │ │ ├── command.log
│ │ │ ├── stderr.log
│ │ │ ├── stdout.log
│ │ │ ├── stdout_stderr.log
│ │ │ └── streams.log
│ │ └── logger_all.log
│ ├── build_2025-07-22_09-27-26
│ │ ├── events.log
│ │ ├── hello_cpp_node
│ │ │ ├── command.log
│ │ │ ├── stderr.log
│ │ │ ├── stdout.log
│ │ │ ├── stdout_stderr.log
│ │ │ └── streams.log
│ │ └── logger_all.log
│ ├── build_2025-07-22_09-30-06
│ │ ├── events.log
│ │ ├── hello_cpp_node
│ │ │ ├── command.log
│ │ │ ├── stderr.log
│ │ │ ├── stdout.log
│ │ │ ├── stdout_stderr.log
│ │ │ └── streams.log
│ │ └── logger_all.log
│ ├── COLCON_IGNORE
│ ├── latest -> latest_build
│ └── latest_build -> build_2025-07-22_09-30-06
└── src
└── hello_cpp_node
├── CMakeLists.txt
├── include
│ └── hello_cpp_node
├── package.xml
└── src
└── hello_cpp_node.cpp
61 directories, 134 files
评论