本文系统配置ubuntu20.04+ros1,是初学者按照ros官网初级教程学习记录,仅作个人学习记录,如有问题可联系本文作者。
附上ros1官网学习教程链接Documentation - ROS Wikihttps://wiki.ros.org/ros2官网学习教程链接Project Governance — ROS 2 Documentation: Rolling documentationhttps://docs.ros.org/en/rolling/The-ROS2-Project/Governance.html
构建软件包
只要安装了这个包的所有系统依赖项,就可以开始编译软件包了。当然如果已经通过apt或者其它软件包管理器安装ROS,那么系统已经安装好了所有的依赖项。
另外如果还没设置环境,需要先soucre一下。在Ubuntu中的操作如下:
$ source /opt/ros/<distro>/setup.bash
如前面学习中所讲,需要将<distro>部分修改为对应自己ROS版本,比如我安装的是ROS Noetic Ninjemys,则上述命令改为:
$ source /opt/ros/noetic/setup.bash
1.使用catkin_make
catkin_make 是一个命令行工具,它简化了标准catkin工作流程。可以认为catkin_make是在标准CMake工作流程中依次调用了cmake和make。
用法如下:
# 在catkin工作空间下 $ catkin_make [make_targets] [-DCMAKE_VARIABLES=...]
如果你不熟悉什么是标准CMake工作流程,可以认为是以下几个步骤。当然以下命令无效不可以直接运行,它只是一个演示CMake工作流程的例子:
# 在CMake工作空间下 $ mkdir build $ cd build $ cmake .. $ make $ make install # (可选)
每个CMake项目都要单独进行这样的步骤。相反,多个catkin项目可以放在工作空间中一起构建,在工作空间中构建零到多个catkin软件包为以下工作流程:
# 在catkin工作空间下 $ catkin_make $ catkin_make install # (可选)
上述命令会构建src目录下的所有catkin项目。该过程遵循REP128,如果你的源代码不在默认位置(catkin_ws/src),比如说存放在了my_src中,那可以这样来使用catkin_make(以下仍为示例,不可直接运行):
# 在catkin工作空间下 $ catkin_make --source my_src $ catkin_make install --source my_src # (可选)
对于catkin_make的高级用法,可以继续参考catkin/commands/catkin_make。
2.开始构建你的软件包
如果现在就要构建自己的代码,可以同时看一下后面C++/Python教程,因为可能需要修改CMakeLists.txt文件。
按照之前的步骤,此时应该已经创建好了一个catkin工作空间和一个名为beginner_tutorials的catkin软件包。现在切换到catkin工作空间并查看src目录:
$ cd ~/catkin_ws/ $ ls src
输出如下:
可以看到一个名为beginner_tutorials的目录,这就是之前用catkin_create_pkg命令创建的。现在可以使用catkin_make来构建它:
$ catkin_make
输出如下:
也可以看到很多cmake和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 #### #### 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" ####(此处个人保留)
catkin_make首先输出它所使用到的每个空间所在的路径,更多关于空间的信息,可以参考REP128和catkin/workspaces。
另外根据这些变量的默认值,有几个目录已经在catkin工作空间中自动生成了,用ls看看:
$ ls
输出如下:
其中build 目录是构建空间的默认位置,同时cmake和make也是在这里被调用来配置和构建你的软件包。而devel目录是开发空间的默认位置, 在安装软件包之前,这里可以存放可执行文件和库。
至此已成功构建了一个ROS软件包,可以继续接下来的学习。