寻找对方包时的cmake包配置

Cmake package configuration when looking for each other's packages

当我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>

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

Circular dependency in subset of packages: module_1, module_2

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