ROS2环境在win11下磕磕绊绊的安装验证完成,官网资料一言难尽,接下来就是重头戏着手使用ROS2开发,首先我们要验证以下开发环境(编译)是否可用。
过程涉及主要指令如下:
#CMD D: #CMD cd D:\ros2_ws #cmd git clone https://github.com/ros2/examples src/examples -b foxy #CMD call C:\opt\ros\foxy\x64\local_setup.bat #ps optional C:\opt\ros\foxy\x64\local_setup.ps1 #Run tests colcon test --merge-install #设置环境变量 call install\setup.bat #PS install\setup.ps1 #终端1 ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function #终端2 ros2 run examples_rclcpp_minimal_publisher publisher_member_function
官方重要提示:
Remember to use a x64 Native Tools Command Prompt for VS 2019 for executing the following command, as we are going to build a workspace.
使用colcon 编译
colcon build --symlink-install --merge-install
编译报错错误
Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules are removed. Run "cmake --help-policy CMP0148" for policy details. Use the cmake_policy command to set the policy and suppress this warning.
原因:CMake 3.12后, FindPythonInterp 及 FindPythonLibs被废弃
解决方法1:
使用 FindPython3、FindPython2、FindPython替换
实例:CMakeLists.txt中修改即可,如下图
#include(FindPythonInterp) include(FindPython3)
解决方法2:安装CMake 3.12,此方法还是会报错
#新版本提示找不到Visual Studio 15 2017 #-G "Visual Studio 16 2019 Win64" #-G "Visual Studio 14 2017 Win64" colcon build --symlink-install --merge-install --cmake-args -G "Visual Studio 15 2017"
解决方法3:升级最新release3.27
下载cmake-3.27.9-windows-x86_64.msi更新
此三个方法我先验证方法2,发现还会报错,就重新安装新版cmake再试一遍发现没有再报错了
使用colcon test 代码测试
CTest测试内容先跳过,不确定我本地有没有Ctest
运行示例代码
#CMD call install\setup.bat #PS install\setup.ps1 #终端1 ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function #终端2 ros2 run examples_rclcpp_minimal_publisher publisher_member_function
运行截图如下
CMake 命令行可以带有一些参数,用于控制和配置 CMake 的行为。这些参数可以在执行 cmake 命令时传递给 CMake。以下是一些常用的 CMake 命令行参数:
-D:用于设置 CMake 变量。例如,-DVAR_NAME=VALUE 可以设置一个 CMake 变量的值
cmake -DVAR_NAME=VALUE path_to_source_directory
-G:用于指定生成器(generator)。生成器决定了 CMake 会生成哪种类型的构建系统文件。例如,-G "Unix Makefiles" 可以指定使用 Unix Makefile 生成器。
cmake -G "Unix Makefiles" path_to_source_directory
-S:选项用于指定源代码目录。这是你的项目的根目录,包含了 CMakeLists.txt 文件,这些文件描述了项目的配置和构建过程。
cmake -S path_to_source_directory
-B: 选项用于指定构建目录。这是 CMake 生成的 Makefile 和其他构建系统文件的输出目录。在这个目录中,你可以运行构建命令,例如 make,来构建你的项目。
cmake -B path_to_build_directory
这将会告诉 CMake 去源代码目录 /path/to/source 查找 CMakeLists.txt 文件,并将生成的构建系统文件输出到 /path/to/build 目录中。一旦配置完成,你可以进入构建目录并运行构建命令来构建你的项目。
--build:用于执行构建过程。例如,--build path_to_build_directory 可以执行构建操作。
cmake --build path_to_build_directory
--config:用于指定构建类型,例如 Debug 或 Release。
cmake --build path_to_build_directory --config Release
一些常用的colcon参数
参考:
build - Build Packages — colcon documentation
colcon提供了很多的参数选项,大家可以去官网查看,这里我不再逐一翻译 ,只是简单枚举一下官网的内容,
目前遇到常用参数:
1.--symlink-install :使用符号链接而不是复制文件,如
以动态链接库为例,会在install目录中使用符号链接,指向build目录下生成的库文件(如 *.so). 没有该选项,则两个目录都会有该库文件
2.--packages-select :只编译指定包,如
colcon build --packages-select autoware_map_msgs vector_map_msgs
3.--packages-ignore : 忽略指定包,同上
4. --continue-on-error :在编译出错之后继续编译其他模块
5. --cmake-args ,--ament-cmake-args, --catkin-cmake-args :传递参数给对应的package
针对cmake参数,常用的有
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_CXX_FLAGS="-O2 -g -Wall "
“-D” --宏定义, 每定义一个就在前边加上"-D",给gcc传递参数
-g debug选项, gdb模式,符号表会保存
-s link选项,删除符号表,这一步会极大减少文件体积
下面为colcon官网上的原英文解释。
build - Build Packages
The build verb is building a set of packages. It is provided by the colcon-core package.
Command line arguments
These common arguments can be used:
executor arguments
event handler arguments
discovery arguments
package selection arguments
mixin arguments
Additionally, the following specific command line arguments can be used:
--build-base BUILD_BASE
The base path for all build directories. The default value is ./build. Each package uses a subdirectory in that base path as its package specific build directory.
--install-base INSTALL_BASE
The base path for all install prefixes. The default value is ./install.
--merge-install
Use the --install-base as the install prefix for all packages instead of a package specific subdirectory in the install base.
Without this option each package will contribute its own paths to environment variables which leads to very long environment variable values.
With this option most of the paths added to environment variables will be the same, resulting in shorter environment variable values.
The disadvantage of using this option is that it doesn’t provide proper isolation between packages. For example declaring a dependency on one package also allows access to resources from other packages installed in the same install prefix (without requiring a declared dependency).
Note: on Windows using cmd this argument should be used for workspaces with many packages otherwise the environment variables might exceed the supported maximum length.
--symlink-install
Use symlinks instead of copying files from the source and build directories where possible.
--test-result-base TEST_RESULT_BASE
The base path for all test results. The default value is the --build-base argument. Each package uses a subdirectory in that base path as its package specific test result directory.
--continue-on-error
Continue building other packages when a package fails to build. Packages recursively depending on the failed package are skipped.
CMake specific arguments
The following arguments are provided by the colcon-cmake package:
--cmake-args [* [* …]]
Pass arbitrary arguments to CMake projects. Arguments matching other options must be prefixed by a space, e.g. --cmake-args " --help".
--cmake-target CMAKE_TARGET
Build a specific target instead of the default target. To avoid packages which don’t have that target causing the build to fail, also pass –cmake-target-skip-unavailable.
--cmake-target-skip-unavailable
Skip building packages which don’t have the target passed to –cmake-target.
--cmake-clean-cache
Remove the CMake cache file CMakeCache.txt from the build directory before proceeding with the build. This implicitly forces a CMake configure step.
--cmake-clean-first
Build the target clean first, then proceed with a regular build. To only invoke the clean target use –cmake-target clean.
--cmake-force-configure
Force CMake configure step.
ROS ament_cmake specific arguments
The following arguments are provided by the colcon-ros package:
--ament-cmake-args [* [* …]]
Pass arbitrary arguments to ROS packages with the build type ament_cmake. Arguments matching other options must be prefixed by a space, e.g. --ament-cmake-args " --help".
ROS catkin specific arguments
The following arguments are provided by the colcon-ros package:
--catkin-cmake-args [* [* …]]
Pass arbitrary arguments to ROS packages with the build type catkin. Arguments matching other options must be prefixed by a space, e.g. --catkin-cmake-args " --help".
--catkin-skip-building-tests
By default the tests target building the tests in catkin packages is invoked. If running colcon test later isn’t intended this can be skipped.
参考连接
https://docs.ros.org/en/foxy/Tutorials/Beginner-Client-Libraries/Colcon-Tutorial.html
https://docs.ros.org/en/foxy/Tutorials/Beginner-CLI-Tools/Configuring-ROS2-Environment.html