查找对方包时的cmake包配置

问题描述

当我catkin_makemodule_one找到包module_twomodule_two找到包module_one时,出现如下错误

-- +++ processing catkin package: 'module_one'
-- ==> add_subdirectory(module_one)
-- Could NOT find module_two (missing: module_two_DIR)
-- Could not find the required component 'module_two'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found.
CMake Error at /opt/ros/melodic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
  Could not find a package configuration file provided by "module_two" with
  any of the following names:

    module_twoConfig.cmake
    module_two-config.cmake

  Add the installation prefix of "module_two" to CMAKE_PREFIX_PATH or set
  "module_two_DIR" to a directory containing one of the above files.  If
  "module_two" provides a separate development package or SDK,be sure it has
  been installed.
Call Stack (most recent call first):
  module_one/CMakeLists.txt:10 (find_package)


-- Configuring incomplete,errors occurred!
See also "/home/ri/workspace/catkin_playground/build/CMakeFiles/CMakeOutput.log".
See also "/home/ri/workspace/catkin_playground/build/CMakeFiles/CMakeError.log".

工作区文件夹树如下

├── build
│   ├── atomic_configure
│   ├── catkin
│   │   └── catkin_generated
│   │       └── version
│   ├── catkin_generated
│   │   ├── installspace
│   │   └── stamps
│   │       └── Project
│   ├── CMakeFiles
│   │   ├── 3.11.0
│   │   │   ├── CompilerIdC
│   │   │   │   └── tmp
│   │   │   └── CompilerIdCXX
│   │   │       └── tmp
│   │   └── CMakeTmp
│   ├── gtest
│   │   ├── CMakeFiles
│   │   └── googlemock
│   │       ├── CMakeFiles
│   │       └── gtest
│   │           └── CMakeFiles
│   ├── module_one
│   │   └── CMakeFiles
│   └── test_results
├── devel
│   └── lib
└── src
    ├── module_one
    └── module_two

module_one 的 CMakeLists.txt 有

find_package(catkin required module_two)

module_two 的 CMakeLists.txt 有

find_package(catkin required module_one)

像上面的项目,
是否有用于相互引用包的 CMakeLists 配置?

解决方法

我试图模仿你的设置:我创建了一个新的工作区,我使用 catkin_create_pkg 创建了两个新包,但我得到了你的错误。当以下某些设置问题未得到解决时,就会发生这种情况:

  • 在 CMakeLists.txt 中,您必须find_package(catkin REQUIRED COMPONENTS ...) 必要的包(如果您使用 C++,请不要忘记 roscpp!)
# In module_1
find_package(catkin REQUIRED COMPONENTS
  roscpp
  module_2
)
  • 在 CMakeLists.txt 中,您还必须声明对您的依赖项进行 catkin(此 CATKIN_DEPENDS 列表是上述 find_package 列表的镜像):
# In module_1
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES mod2
  CATKIN_DEPENDS roscpp module_2
#  DEPENDS system_lib
)
  • 在 package.xml 中,您还需要一个用于该模块的 <depend>
<!-- In module_1 -->
<depend>module_2</depend>

如果你这样做了,那么错误就会消失。但是你有一个新的错误:

包子集中的循环依赖:module_1、module_2

我建议您重新构建代码以避免循环依赖,方法是组合包,或者如果您更喜欢小包,则将公共依赖项提取到第三个包中。