4. Building a ROS Package

文章详细介绍了如何在ROS环境中使用catkin_make命令来构建和安装包。首先确保所有系统依赖项已安装,然后通过source环境设置文件。catkin_make简化了标准的CMake工作流程,允许在一个catkin工作空间中一起构建多个项目。文章提供了一步步的指令,包括在catkin工作空间中创建和构建名为beginner_tutorials的示例包。

Building Packages

As long as all of the system dependencies of your package are installed, we can now build your new package.

Note: If you installed ROS using apt or some other package manager, you should already have all of your dependencies.

Before continuing remember to source your environment setup file if you have not already. On Ubuntu it would be something like this:

# source /opt/ros/%YOUR_ROS_DISTRO%/setup.bash
$ source /opt/ros/kinetic/setup.bash             # For Kinetic for instance

Using catkin_make

catkin_make is a command line tool which adds some convenience to the standard catkin workflow. You can imagine that catkin_make combines the calls to cmake and make in the standard CMake workflow.

Usage:

# In a catkin workspace
$ catkin_make [make_targets] [-DCMAKE_VARIABLES=...]

For people who are unfamiliar with the standard CMake workflow, it breaks down as follows:

Note: If you run the below commands it will not work, as this is just an example of how CMake generally works.

# In a CMake project
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make install  # (optionally)

This process is run for each CMake project. In contrast catkin projects can be built together in workspaces. Building zero to many catkin packages in a workspace follows this work flow:

# In a catkin workspace
$ catkin_make
$ catkin_make install  # (optionally)

The above commands will build any catkin projects found in the src folder. This follows the recommendations set by REP128. If your source code is in a different place, say my_src then you would call catkin_make like this:

Note: If you run the below commands it will not work, as the directory my_src does not exist.

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

For more advanced uses of catkin_make see the documentation: catkin/commands/catkin_make

Building Your Package

If you are using this page to build your own code, please also take a look at the later tutorials (C++)/(Python) since you may need to modify CMakeLists.txt.

You should already have a catkin workspace and a new catkin package called beginner_tutorials from the previous tutorial, Creating a Package. Go into the catkin workspace if you are not already there and look in the src folder:

$ cd ~/catkin_ws/
$ ls src
  • beginner_tutorials/  CMakeLists.txt@  

You should see that there is a folder called beginner_tutorials which you created with catkin_create_pkg in the previous tutorial. We can now build that package using catkin_make:

$ catkin_make

You should see a lot of output from cmake and then make, which should be similar to this:

  • 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
    ####
    #### 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"
    ####
    -- The C compiler identification is GNU 4.2.1
    -- The CXX compiler identification is Clang 4.0.0
    -- Checking whether C compiler has -isysroot
    -- Checking whether C compiler has -isysroot - yes
    -- Checking whether C compiler supports OSX deployment target flag
    -- Checking whether C compiler supports OSX deployment target flag - yes
    -- Check for working C compiler: /usr/bin/gcc
    -- Check for working C compiler: /usr/bin/gcc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Using CATKIN_DEVEL_PREFIX: /tmp/catkin_ws/devel
    -- Using CMAKE_PREFIX_PATH: /opt/ros/kinetic
    -- This workspace overlays: /opt/ros/kinetic
    -- Found PythonInterp: /usr/bin/python (found version "2.7.1") 
    -- Found PY_em: /usr/lib/python2.7/dist-packages/em.pyc
    -- Found gtest: gtests will be built
    -- catkin 0.5.51
    -- BUILD_SHARED_LIBS is on
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    -- ~~  traversing packages in topological order:
    -- ~~  - beginner_tutorials
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    -- +++ add_subdirectory(beginner_tutorials)
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/user/catkin_ws/build
    ####
    #### Running command: "make -j4" in "/home/user/catkin_ws/build"
    ####

Note that catkin_make first displays what paths it is using for each of the 'spaces'. The spaces are described in the REP128 and by documentation about catkin workspaces on the wiki: catkin/workspaces. The important thing to notice is that because of these default values several folders have been created in your catkin workspace. Take a look with ls:

$ ls
  • build
    devel
    src

The build folder is the default location of the build space and is where cmake and make are called to configure and build your packages. The devel folder is the default location of the devel space, which is where your executables and libraries go before you install your packages.

Now that you have built your ROS package let's talk more about ROS Nodes.

(base) casbot@casbot:~$ sudo apt install ros-noetic-autolabor -y Reading package lists... Done Building dependency tree... Done Reading state information... Done E: Unable to locate package ros-noetic-autolabor (base) casbot@casbot:~$ sudo apt update && sudo apt install -y \ python3 python3-pip git cmake build-essential \ libboost-all-dev libeigen3-dev Ign:1 https://mirror.autolabor.com.cn/ros-noetic jammy InRelease Hit:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports jammy InRelease Hit:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports jammy-updates InRelease Hit:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports jammy-backports InRelease Ign:5 https://mirrors.ustc.edu.cn/ros/ubuntu jammy InRelease Hit:6 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports jammy-security InRelease Err:7 https://mirrors.ustc.edu.cn/ros/ubuntu jammy Release 404 Not Found [IP: 202.141.176.110 443] Get:8 http://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu jammy InRelease [4682 B] Hit:9 http://packages.ros.org/ros/ubuntu focal InRelease Ign:1 https://mirror.autolabor.com.cn/ros-noetic jammy InRelease Ign:1 https://mirror.autolabor.com.cn/ros-noetic jammy InRelease Err:1 https://mirror.autolabor.com.cn/ros-noetic jammy InRelease Could not resolve 'mirror.autolabor.com.cn' Reading package lists... Done E: The repository 'https://mirrors.ustc.edu.cn/ros/ubuntu jammy Release' does not have a Release file. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details. (base) casbot@casbot:~$ # 备份原始源列表 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak # 替换为阿里云镜像源(解决404错误) sudo sed -i 's/cn.archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list # 添加正确的ROS源(使用清华镜像) sudo sh -c 'echo "deb [trusted=yes] https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list' (base) casbot@casbot:~$ # 更新软件包列表 sudo apt update # 修复损坏的依赖(解决libicu-dev问题) sudo apt --fix-broken install -y # 安装必要的依赖库 sudo apt install -y libicu70=70.1-2 libicu-dev python3-rosdep Ign:1 https://mirror.autolabor.com.cn/ros-noetic jammy InRelease Hit:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports jammy InRelease Hit:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports jammy-updates InRelease Hit:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports jammy-backports InRelease Hit:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports jammy-security InRelease Ign:6 https://mirrors.ustc.edu.cn/ros/ubuntu jammy InRelease Get:7 http://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu jammy InRelease [4682 B] Err:8 https://mirrors.ustc.edu.cn/ros/ubuntu jammy Release 404 Not Found [IP: 202.141.176.110 443] Ign:1 https://mirror.autolabor.com.cn/ros-noetic jammy InRelease Ign:9 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy InRelease Ign:10 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy Release Ign:11 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main arm64 Packages Ign:12 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main all Packages Ign:1 https://mirror.autolabor.com.cn/ros-noetic jammy InRelease Ign:13 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main Translation-en Ign:11 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main arm64 Packages Ign:12 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main all Packages Ign:13 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main Translation-en Ign:11 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main arm64 Packages Ign:12 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main all Packages Ign:13 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main Translation-en Ign:11 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main arm64 Packages Ign:12 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main all Packages Ign:13 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main Translation-en Ign:11 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main arm64 Packages Ign:12 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main all Packages Ign:13 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main Translation-en Ign:11 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main arm64 Packages Ign:12 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main all Packages Ign:13 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main Translation-en Err:11 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main arm64 Packages 404 Not Found [IP: 101.6.15.130 443] Ign:12 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main all Packages Ign:13 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu jammy/main Translation-en Err:1 https://mirror.autolabor.com.cn/ros-noetic jammy InRelease Could not resolve 'mirror.autolabor.com.cn' Reading package lists... Done E: The repository 'https://mirrors.ustc.edu.cn/ros/ubuntu jammy Release' does not have a Release file. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details. Reading package lists... Done Building dependency tree... Done Reading state information... Done 0 upgraded, 0 newly installed, 0 to remove and 13 not upgraded. Reading package lists... Done Building dependency tree... Done Reading state information... Done python3-rosdep is already the newest version (0.26.0-1). Suggested packages: icu-doc The following NEW packages will be installed: icu-devtools libicu-dev The following packages will be DOWNGRADED: libicu70 0 upgraded, 2 newly installed, 1 downgraded, 0 to remove and 13 not upgraded. E: Packages were downgraded and -y was used without --allow-downgrades.
07-23
[sudo] password for alex: Get:1 http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal InRelease [4,679 B] Err:1 http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal InRelease The following signatures were invalid: EXPKEYSIG F42ED6FBAB17C654 Open Robotics <info@osrfoundation.org> Hit:3 http://mirrors.ustc.edu.cn/ubuntu focal InRelease Hit:4 http://mirrors.ustc.edu.cn/ubuntu focal-updates InRelease Hit:5 http://mirrors.ustc.edu.cn/ubuntu focal-backports InRelease Hit:2 https://packages.microsoft.com/repos/vscode stable InRelease Hit:6 http://mirrors.ustc.edu.cn/ubuntu focal-security InRelease Get:7 http://packages.ros.org/ros2/ubuntu focal InRelease [4,685 B] Err:7 http://packages.ros.org/ros2/ubuntu focal InRelease The following signatures were invalid: EXPKEYSIG F42ED6FBAB17C654 Open Robotics <info@osrfoundation.org> Reading package lists... Done W: GPG error: http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal InRelease: The following signatures were invalid: EXPKEYSIG F42ED6FBAB17C654 Open Robotics <info@osrfoundation.org> E: The repository 'http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal InRelease' is no longer signed. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details. W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://packages.ros.org/ros2/ubuntu focal InRelease: The following signatures were invalid: EXPKEYSIG F42ED6FBAB17C654 Open Robotics <info@osrfoundation.org> Reading package lists... Done Building dependency tree Reading state information... Done 0 upgraded, 0 newly installed, 0 to remove and 940 not upgraded. Reading package lists... Done Building dependency tree Reading state information... Done 0 upgraded, 0 newly installed, 0 to remove and 940 not upgraded.
06-14
han@han:~$ sudo apt update sudo apt upgrade sudo apt install ros-jazzy-desktop Hit:1 https://mirrors.bfsu.edu.cn/ubuntu noble InRelease Hit:2 https://mirrors.bfsu.edu.cn/ubuntu noble-updates InRelease Hit:3 https://mirrors.bfsu.edu.cn/ubuntu noble-backports InRelease Hit:4 https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal InRelease Hit:5 http://security.ubuntu.com/ubuntu noble-security InRelease Get:6 https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu noble InRelease [4,676 B] Err:6 https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu noble InRelease The following signatures couldn't be verified because the public key is not available: NO_PUBKEY F42ED6FBAB17C654 Reading package lists... Done W: GPG error: https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu noble InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY F42ED6FBAB17C654 E: The repository 'https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu noble InRelease' is not signed. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details. Reading package lists... Done Building dependency tree... Done Reading state information... Done Calculating upgrade... Done The following package was automatically installed and is no longer required: nvidia-firmware-550-550.120 Use 'sudo apt autoremove' to remove it. The following upgrades have been deferred due to phasing: ubuntu-drivers-common 0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded. Reading package lists... Done Building dependency tree... Done Reading state information... Done E: Unable to locate package ros
07-06
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值