catkin tools/make 编译小结

一、catkin tools

安装  sudo apt-get install python-catkin-tools

  • catkin build    -BuildPackages
  • catkin clean    -Clean Build Products
  • catkin config   -Configure a Workspace
  • catkin create   -Create Packages
  • catkin env      -Environment Utility
  • catkin init     -Intialize a Workspace
  • catkin list     -List Package Info
  • catkin locate   -Locate Directories
  • catkin profile  -Manage Profiles

1. Initializing Workspaces初始化工作空间

初始化具有默认布局的工作区(src/build/devel)在当前目录中:

catkin init
catkin init --workspace .
catkin config --init
mkdir src && catkin build

在不同目录中具有默认布局:

catkin init --workspace /tmp/path/to/my_catkin_ws

显式扩展另一个工作区:

catkin config --init --extend /opt/ros/indigo

初始化具有源空间other_src的工作空间:

 catkin config --init --source-space other_src

or a workspace with builddevel, and install space ending with the suffix _alternate:

catkin config --init --space-suffix _alternate

 

2. Configuring Workspaces配置工作空间

查看当前配置:

catkin config

设置和复位CMake选项:

catkin config --cmake-args -DENABLE_CORBA=ON -DCORBA_IMPLEMENTATION=OMNIORB
catkin config --no-cmake-args

将安装切换到指定的安装空间:

catkin config --install

3. Building Packages编译包

[1]Build all the packages:

catkin build

...one at a time, with additional debug output[一次一个,额外的调试输出]:

catkin build -p 1

...迫使CMake重新配置每一个:

catkin build --force-cmake

[2]Build a specific package and its dependencies: 编译一个包和依赖

catkin build my_package

... or ignore its dependencies:

 catkin build my_package --no-deps

[3]Build packages包含当前的工作目录:

catkin build --this

... but don’t rebuild its dependencies:  不重新编译他的依赖

 catkin build --this --no-deps

[4]Build packages有额外的CMake参数:

catkin build --cmake-args -DCMAKE_BUILD_TYPE=Debug

... and save them to be used for the next build:

catkin build --save-config --cmake-args -DCMAKE_BUILD_TYPE=Debug

Build all packages 在给定目录中:

catkin build $(catkin list -u /path/to/folder)

... or in the current folder:

catkin build $(catkin list -u .)

4.Cleaning Build Products

清理 the build, devel, and install spaces (if they exist):

catkin clean

... or just the build space:

catkin clean --build

... or just clean a single package:

 catkin clean PKGNAME

... or just delete the build directories(生成目录) for packages which have been disabled or removed:

catkin clean --orphans

5.Create Packages

快速创建workspace中的catkin packages:

Creates catkin workspace resources like packages:

catkin create pkg

6.Environment Utility

 It can be used to both print the current environment variables and run a command in a modified (修改的)environment.It is primarily used in the build stage command reproduction.

Run an arbitrary command in a modified environment:

catkin env [-h] [-i] [-s]
           [NAME=VALUE [NAME=VALUE ...]] [COMMAND] [ARG [ARG ...]]

7.List Package Info

 It is used to find and list information about catkin packages. By default(默认情况下), it will list the packages in the workspace containing the current working directory. It can also be used to list the packages in any other arbitrary directory.

Lists catkin packages in the workspace or other arbitray folders:
catkin list [-h] [--workspace WORKSPACE] [--profile PROFILE]
            [--deps | --rdeps] [--depends-on [PKG [PKG ...]]]
            [--rdepends-on [PKG [PKG ...]]] [--this] [--quiet]
            [--unformatted]

8.Locate Directories定位路径

 It can be used to locate important locations in the workspace such as the active sourcebuilddevel, and install spaces, and package directories in the workspace.

Get the paths to various locations in a workspace:
catkin locate [-h] [--workspace WORKSPACE] [--profile PROFILE] [-e]
              [-r] [-q] [-s | -b | -d | -i] [--shell-verbs]
              [--examples]
              [PACKAGE]

9.Manage Profiles

创建所有应用程序服务器运行时环境。该命令创建概要文件,即定义 Deployment Manager、定制概要文件或独立应用程序服务器的运行时环境的文件集合。

It has several sub-commands for profile management.有多个子命令用于配置文件管理

Manage config profiles for a catkin workspace:

catkin profile [-h] [--workspace WORKSPACE]
                      {list,set,add,rename,remove} ...

sub-command help:
    list                List the available profiles.
    set                 Set the active profile by name.
    add                 Add a new profile by name.
    rename              Rename a given profile.
    remove              Remove a profile by name.

 

10.Controlling Color Display控制彩色显示

Disable colors when building in a shell that doesn’t support it (like IDEs):

catkin --no-color build

... or enable it for shells that don’t know they support it:

catkin --force-color build

11.Profile Cookbook

Create “Debug” and “Release” profiles and then build them in independent build and devel spaces:

catkin config --profile debug -x _debug --cmake-args -DCMAKE_BUILD_TYPE=Debug
catkin config --profile release -x _release --cmake-args -DCMAKE_BUILD_TYPE=Release
catkin build --profile debug
catkin build --profile release

快速从头开始建立一个包Quickly build a package from scratch to make sure all of its dependencies are satisfied, then clean it:

catkin config --profile my_pkg -x _my_pkg_test
catkin build --profile my_pkg my_pkg
catkin clean --profile my_pkg --all

12.Manipulating Workspace Chaining操纵空间链接

Change from implicit to explicit chaining[从隐性到显性链接:]:

catkin clean
catkin config --extend /opt/ros/indigo

Change from explicit to implicit chaining:

catkin clean
catkin config --no-extend

13.Building With Other Job Servers

Build with distcc:

CC="distcc gcc" CXX="distcc g++" catkin build -p$(distcc -j) -j$(distcc -j) --no-jobserver

总结:

  1. catkin init – Initialize a Workspace初始化
  2. catkin config – Configure a Workspace配置
  3. catkin build – Build Packages编译
  4. catkin clean – Clean Build Products
  5. catkin create – Create Packages
  6. catkin env – Environment Utility
  7. catkin list – List Package Info
  8. catkin locate – Locate Directories
  9. catkin profile – Manage Profiles

 

二.catkin_make   和cmake 关系

1、简介

程序在cmake编译是这样的流程, cmake指令依据你的CMakeLists.txt 文件,生成makefiles文件,make再依据此makefiles文件编译链接生成可执行文件.catkin_make是将cmake与make的编译方式做了一个封装的指令工具, 规范了工作路径与生成文件路径.

cmake 的编译流程:

$ mkdir build  
$ cd build  
$ cmake ..  
$ make  
$ make install  # (可选)  

catkin make的编译流程:

# In a catkin workspace  
$ catkin_make  
$ catkin_make install  # (可选)  

如果源码不在默认工作空间,需要指定编译路径:  

# In a catkin workspace  
$ catkin_make --source my_src  
$ catkin_make install --source my_src  # (optionally) 

2.catkin_make

1.catkin_make的默认路径

catkin_make运行后终端输出文件部分解析  
  
#基本路径  
Base path: /home/user/catkin_ws  
Source space: /home/user/catkin_ws/src   
Build space: /home/user/catkin_ws/build  
Devel space: /home/user/catkin_ws/devel  
Install space: /home/user/catkin_ws/install  
  
  
#catkin_make 封装运行中cmake运行的情况  
Running command: "cmake /home/user/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel   
-DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install" in "/home/user/catkin_ws/build"    
  
#编译工具查找  
-- Using CATKIN_DEVEL_PREFIX: /tmp/catkin_ws/devel  
-- Using CMAKE_PREFIX_PATH: /opt/ros/groovy  
-- This workspace overlays: /opt/ros/groovy  
  
#编译的包  
#catkin_make 封装运行中make运行的情况

2.layout :ros工作空间文件系统结构

 workspace_folder/        --WORKSPACE  工作空间
      src/                   --SOURCE SPACE  源空间
        CMakeLists.txt/      --This is symlinked to catkin/cmake/toplevel.cmake  
        package_1/  
          CMakeLists.txt  
          package.xml  
        ...  
        package_n/  
          CMakeLists.txt  
          package.xml  
      build/                 --BUILD SPACE 编译空间 (this is where build system is invoked, not necessarily within workspace)  
        CATKIN_IGNORE        --Marking the folder to be ignored when crawling for packages (necessary when source   
                                space is in the root of the workspace, the file is emtpy)  
                                #此选项可用于忽略某个包编译  
     devel/                 --DEVEL SPACE 开发空间(targets go here, parameterizable, but defaults to peer of Build Space)  
                               # 生成二值 库 可执行文件   
        bin/  
        etc/  
        /include/  
        lib/  
        share/  
        .catkin              --Marking the folder as a development space (the file contains a semicolon separated list of Source space paths)  
                                #  
        env.bash  
        setup.bash  
        setup.sh  
        ...  
      install/               --INSTALL SPACE 安装空间[-DCMAKE_INSTALL_PREFIX=/any/directorycmake默认是/usr/local](this is where installed targets for test installations go, not necessarily within workspace)  
        bin/  
        etc/  
        include/  
        lib/  
        share/  
        .catkin              --Marking the folder as an install space (the file is emtpy)  
        env.bash  
        setup.bash    -- Environment setup file for Bash shell  
        setup.sh      -- Environment setup file for Bourne shell   
        ...  
      
      
    /  系统安装空间         /opt/ros/ROSDISTRO
      opt/  
        ros/  
          groovy/  
            setup.bash -- Environment setup file for Bash shell  
            setup.sh   -- Environment setup file for Bourne shell  
            setup.zsh  -- Environment setup file for zshell  
            ...  

结果空间    source RESULT-SPACE/setup.sh     类似扫描安装空间与开发空间,替换系统通用下的对应文件.

3.Overlays

catkin支持包的逐层覆盖, 当前最高,其它依据source的顺序逐层变高, 高层可覆盖低层. 

Example 4: Overlaying workspace 3 on top of local workspace2 install space on top of workspace1 devel space on top of system install  
      
    cd ~/workspace2/build  
    cmake -DCMAKE_INSTALL_PREFIX=~/ws2_installed ../src  
    make  
    make install  
      
    source ~/ws2_installed/setup.bash  
      
    cd ~/workspace3/build  
    cmake ../src  
    make  

ros 环境变量设置 可以参考 .bashrc文件

 #  slam_ws
source /opt/ros/indigo/setup.bash    
source /home/yhzhao/slam_ws/devel/setup.bash    
    
export ROS_PACKAGE_PATH=~/slam_ws/src:$ROS_PACKAGE_PATH    
export ROS_WORKSPACE=~/slam_ws/src   

 4.catkin_make编译指定的包

$ catkin_make -DCATKIN_WHITELIST_PACKAGES="package1;package2"

恢复编译所有的包

$ catkin_make -DCATKIN_WHITELIST_PACKAGES=""

3. 对这句命令进行解释:

catkin build rovio --cmake-args -DCMAKE_BUILD_TYPE=Release -DMAKE_SCENE=ON

catkin build – Build Packages,编译rovio包

如果工作空间未初始化,可以自动完成工作空间初始化 (source ~/catkin_ws/devel/setup.bash)
It is used to build one or more packages in a catkin workspace. If a workspace is not yet initialized(初始化), build can initialize it with the default configuration(默认配置), but only if it is called from the workspace root. Specific workspaces can also be built from arbitrary working directories with the --workspace option.

--cmake-args    #cmake参数
-DCMAKE_BUILD_TYPE=Release

使用 CMake我们可以生成 debug 版和 release 版的程序。debug 版的项目生成的可执行文件需要有调试信息并且不需要进行优化,而 release 版的不需要调试信息但需要优化。这些特性在 gcc/g++ 中是通过编译时的参数来决定的,如果将优化程度调到最高需要设置参数-O3,最低是 -O0 即不做优化;添加调试信息的参数是 -g -ggdb ,如果不添加这个参数,调试信息就不会被包含在生成的二进制文件中。

CMake 中有一个变量 CMAKE_BUILD_TYPE ,可以的取值是 Debug、 Release、 RelWithDebInfo 和 MinSizeRel。当这个变量值为 Debug 的时候,CMake 会使用变量 CMAKE_CXX_FLAGS_DEBUG 和 CMAKE_C_FLAGS_DEBUG 中的字符串作为编译选项生成 Makefile ,当这个变量值为 Release 的时候,工程会使用变量 CMAKE_CXX_FLAGS_RELEASE 和 CMAKE_C_FLAGS_RELEASE 选项生成 Makefile。

现假设项目中只有一个文件 main.cpp ,下面是一个可以选择生成 debug 版和 release 版的程序的 CMakeList.txt :

 PROJECT(main)
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 
 SET(CMAKE_SOURCE_DIR .) 
 SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")   
 SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
 AUX_SOURCE_DIRECTORY(. DIR_SRCS) 
 ADD_EXECUTABLE(main ${DIR_SRCS}) 

 

你可能感兴趣的:(ROS)