天天看點

ROS2進階:colcon的初步使用--‘colcon‘ is not recognized

系統安裝路徑:C:\opt\ros\galactic

系統安裝參考:​​ROS2在windows上的安裝​​。

如果你是按ROS官網的辦法安裝的,路徑可能會有所不同,比如按

​​Installing ROS 2 on Windows — ROS 2 Documentation: Galactic documentation​​

此時的路徑會是,

C:\dev\ros2_galactic

不管你按哪個辦法安裝,使用起來都差不多。

首先檢查一下你是否安裝了必要的工具,例如,你可能會碰到這樣的報錯,

ERROR: 'colcon' is not recognized as an internal or external command,

colcon是依賴python的,那說明你沒有安裝colcon腳本,或者,你沒有在系統路徑中添加這個:C:\Python38\Scripts。下面的把一些常用的腳本例在下面,

pip install -U colcon-common-extensions
pip install -U vcstool      

貌似各個版本的ROS2文檔都有些差別,比如eloquent就單獨列出了這些指令,但foxy和galactic就沒有,

​​Building ROS 2 on Windows — ROS 2 Documentation: Eloquent documentation​​

安裝好了之後,你就可以正常使用colcon了,檢查一下,

C:>colcon
usage: C:\Python38\Scripts\colcon [-h] [--log-base LOG_BASE] [--log-level LOG_LEVEL]
                                  {build,extension-points,extensions,graph,info,list,metadata,test,test-result,version-check} ...

Error: No verb provided      

另外,如果你碰到找不到vc之類的問題,通常是因為沒有啟動VS2019或VS2017的環境,

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64      

 為了避免這個麻煩,我通常都是直接在VS2019指令視窗"x64 Native Tools Command Prompt for VS 2019" 中進行操作。

什麼是colcon?

在ROS2中的建構工具是colcon,這非常類似我們在ROS中使用的catkin_make。

下面我們打開一個指令視窗,

call C:\opt\ros\galactic\x64\setup.bat

然後,建立一個路徑

~> mkdir  examples_ws\src
~> cd  examples_ws\src
~examples_ws\src>  git clone -b galactic-devel https://github.com/ros2/examples.git
~examples_ws\src>  cd ..
~examples_ws>  colcon build      

另外,我們也可以試一下那個demos

~> mkdir  demos_ws\src
~> cd  demos_ws\src
~demos_ws\src>  git clone -b galactic-devel https://github.com/ros2/demos.git
~demos_ws\src>  cd ..
~demos_ws>  colcon build      

比如我第一次建構的結果是這樣的,

D:\ros2prj\demo_ws>colcon build
[0.755s] root DEBUG Using proactor: IocpProactor
Starting >>> action_tutorials_interfaces
Starting >>> dummy_map_server
Starting >>> dummy_sensors
Starting >>> pendulum_msgs
Starting >>> composition
Starting >>> demo_nodes_cpp
Starting >>> demo_nodes_cpp_native
Starting >>> demo_nodes_py
Starting >>> image_tools
Starting >>> intra_process_demo
Starting >>> lifecycle
Starting >>> logging_demo
Finished <<< demo_nodes_py [1.61s]
Starting >>> quality_of_service_demo_cpp
Finished <<< dummy_map_server [4.20s]
Starting >>> quality_of_service_demo_py
Finished <<< dummy_sensors [4.80s]
Starting >>> topic_monitor
Finished <<< lifecycle [5.20s]
Starting >>> topic_statistics_demo
Finished <<< quality_of_service_demo_py [1.34s]
Starting >>> dummy_robot_bringup
Finished <<< topic_monitor [1.34s]
Finished <<< pendulum_msgs [7.11s]
Starting >>> pendulum_control
Finished <<< action_tutorials_interfaces [7.36s]
Starting >>> action_tutorials_cpp
Starting >>> action_tutorials_py
Finished <<< dummy_robot_bringup [1.99s]
Finished <<< action_tutorials_py [2.16s]
Failed   <<< demo_nodes_cpp_native [10.1s, exited with code 1]
Aborted  <<< image_tools [11.6s]
Aborted  <<< demo_nodes_cpp [12.0s]
Aborted  <<< composition [12.7s]
Aborted  <<< intra_process_demo [13.9s]
Aborted  <<< quality_of_service_demo_cpp [13.3s]
Aborted  <<< topic_statistics_demo [10.8s]
Aborted  <<< logging_demo [16.0s]
Aborted  <<< pendulum_control [9.20s]
Aborted  <<< action_tutorials_cpp [11.2s]

Summary: 10 packages finished [18.9s]
  1 package failed: demo_nodes_cpp_native
  9 packages aborted: action_tutorials_cpp composition demo_nodes_cpp image_tools intra_process_demo logging_demo pendulum_control quality_of_service_demo_cpp topic_statistics_demo      

官方介紹

官方資料可以去這裡,

​​colcon - collective construction — colcon documentation​​

或者

​​GitHub - colcon/colcon.readthedocs.org​​

一些常用的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"​

​.