Package the ros function package as a deb installation package and install it successfully (super detailed analysis)

Article directory

    • 1. Modify the `CMakeLists.txt` file in the package to be packaged, and add the following corresponding `install` statement according to your own packaging requirements
      • a. Package executable or library files:
      • b. Package header file:
      • c. Pack srv, msg, cfg files:
      • f. Package script file:
      • g. Pack the whole folder of other parts:
    • 2. Install the packaging tool for packaging
      • 1. Install the packaging tool bloom
      • 2. Pack
    • 3. Install the .deb package generated by packaging
    • 4. Possible errors

1. Modify the CMakeLists.txt file in the package to be packaged, and add the following corresponding install statement according to your own packaging requirements

a. Package executable or library files:

Add the target name after TARGETS, such as the following aruco_ros_utils

 install(TARGETS aruco_ros_utils
   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )

b. Package header file:

Method 1: By directory (the .h file under include/ and the folder containing the .h file will be packaged)

install(DIRECTORY include/
   DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}
   FILES_MATCHING PATTERN "*.h"
)

Method 2: by file

install(FILES include/${PROJECT_NAME}/parser_base.h
              include/${PROJECT_NAME}/wr_ls_common.h
              include/${PROJECT_NAME}/wr_ls_sensor_frame.h
        DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
 )

c. Pack srv, msg, cfg files:

There is no need to add an install statement, but make sure your CMakeLists.txt has something like this:

For msg, srv files

set(PACKAGE_DEPENDENCIES
  geometry_msgs
  std_msgs
)

find_package(catkin REQUIRED COMPONENTS message_generation ${<!-- -->PACKAGE_DEPENDENCIES})

add_message_files(
  DIRECTORY msg
  FILES
    BagfileProgress.msg
    HistogramBucket.msg
)

add_service_files(
  DIRECTORY srv
  FILES
    FinishTrajectory.srv
    GetTrajectoryStates.srv
)

generate_messages(
  DEPENDENCIES
    ${<!-- -->PACKAGE_DEPENDENCIES}
)

catkin_package(
  CATKIN_DEPENDS
    ${<!-- -->PACKAGE_DEPENDENCIES}
    message_runtime
)

For cfg files

generate_dynamic_reconfigure_options(
  cfg/ArucoThreshold.cfg
)

f. Package script file:

Add script directory after PROGRAMS, such as packaging scripts/tf_remove_frames.py script

install(PROGRAMS scripts/tf_remove_frames.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

g. Package the entire folder of other parts:

Add the directory name after DIRECTORY, such as packaging urdf, meshes, launch, plugins, rviz folders

install(DIRECTORY urdf meshes launch plugins rviz
  DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)

Second, install the packaging tool for packaging

1. Install the packaging tool bloom

sudo apt-get install python-bloom

If an error is reported, change it to

sudo apt-get install python3-bloom

2. Packaging

Run the following command under the path of the package that needs to be packaged (for example, to package usb_cam under ~/catkin_tmp/src/usb_cam), and then a debian folder will be generated in the current path, which is Packaging Formulate packaging rules

bloom-generate rosdebian --os-name ubuntu --ros-distro noetic

Then run the following command in the current path (for example, to package usb_cam under ~/catkin_tmp/src/usb_cam) to generate a .deb installation package

fakeroot debian/rules binary

3. Install the generated .deb package

The .deb installation package is usually generated in the ~/catkin_ws/src/ directory, if there are two packages, select the first one ending with .deb to install
For example, install the .deb package generated in the previous step under the ~/catkin_tmp/src/ path

cd ~/catkin_tmp/src/
sudo dpkg -i ros-noetic-usb-cam_0.3.7-0focal_amd64.deb

4. Possible errors

1. Run bloom-generate rosdebian --os-name ubuntu --ros-distro noetic to report an error

socket.timeout: The read operation timed out (https://raw.githubusercontent.com/ros/rosdistro/dc8db384055251541a1e277b4e02c5f213350163/index-v4.yaml)

solve: Just make sure that ubuntu can connect to the external network or change to a better network and try a few more times
2. Run fakeroot debian/rules binary to report an error

dpkg-shlibdeps: error: no dependency information found for /usr/local/lib/libopencv_imgproc.so.3.4 (used by debian/ros-noetic-usb-cam/opt/ros/noetic/lib/usb_cam/usb_cam_node )

Because these packages are installed from your source code, not through the dpkg method (or sudo apt install method), the system cannot find the dependencies.
solve:
Method 1 (recommended): continue to enter the following command in the current path to regenerate the .deb package

dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info
fakeroot debian/rules binary

Method 2: Uninstall the package that was originally installed through the source code, and package the dependent package that was originally installed through the source code into a .deb package. After installation, run this command fakeroot debian/rules binary to regenerate the .deb Bag
Method 3: Uninstall the package originally installed through the source code, and change to sudo apt install lib*-dev to install the package originally installed through the source code (* is the package name of the dependent package)
Method 4: Uninstall the package originally installed through source code, and change the installation path of the dependent package originally installed through source code from /usr/local to usr/ installation.
Add set(CMAKE_INSTALL_PREFIX /usr) in the CMakeLists.txt file and re-start cmake .., make, sudo make install

3. An error is reported when installing the packaged .deb

dpkg: dependency problems prevent configuration of ros-noetic-usb-cam:
 ros-noetic-usb-cam depends on v4l-utils; however:
  Package v4l-utils is not installed.

Caused by not installing the dependent library v4l-utils in the system
solve: install dependent library sudo apt install package name or sudo apt install lib package name-dev or sudo apt install ros-noetic-package name (any one of the three can run)
For v4l-utils is the following command
sudo apt install v4l-utils or sudo apt install libv4l-utils-dev or sudo apt install ros-noetic-v4l-utils
`
If you rely on a custom message package, you need to package the custom message package as a .deb package first, and then install the .deb package. Refer to this article