cmake CMP0002编译具有相同目标的不同子项目时出错

h5qlskok  于 2022-12-18  发布在  其他
关注(0)|答案(2)|浏览(184)

我有很多子文件夹

home
|
|-library1
|-library2
|
|-libraryn

每个子文件夹包含一个完整的库,可以自己编译(每个库有一个不同的mantainer)。到目前为止,它的工作正常,我编译他们使用脚本。
现在我需要创建另一个依赖于现有库的库,为此,我在home文件夹下创建了一个CMakeLists.txt,使用add_subdirectory命令可以编译所有库。
我有类似

cmake_minimum_required (VERSION 2.8)

add_subdirectory(library1)
add_subdirectory(library2)
...
add_subdirectory(libraryn)

当我尝试执行cmake时,我获得了以下各种库的错误:

CMake Error at libraryY/CMakeLists.txt:63 (add_custom_target):
  add_custom_target cannot create target "doc" because another target with
  the same name already exists.  The existing target is a custom target
  created in source directory
  "/path/to/libraryX".  See
  documentation for policy CMP0002 for more details.

这是因为在每个库中我们创建了一个doc目标来编译库本身的Doxygen文档,当libraryes一个一个编译时它工作得很好,但是对于master CMakeLists.txt我似乎做不到。

# Create doc target for doxygen documentation compilation.
find_package (Doxygen)
if (DOXYGEN_FOUND)
  set (Doxygen_Dir ${CMAKE_BINARY_DIR}/export/${Library_Version}/doc)
  # Copy images folder
  file (GLOB IMAGES_SRC "images/*")
  file (COPY ${IMAGES_SRC} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/images)
  configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  add_custom_target (doc
    ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "Generating doxygen documentation" VERBATIM
  )
else (DOXYGEN_FOUND)
  message (STATUS "Doxygen must be installed in order to compile doc")
endif (DOXYGEN_FOUND)

有没有一种方法可以在不修改这个目标的情况下立即编译这些项目?

lkaoscv7

lkaoscv71#

如果您不想修改任何内容,就可以将所有这些项目作为子项目来构建,那么您可以使用ExternalProject_Add来构建和安装依赖项。

选项

或者,您可以使用option命令从构建中排除doc目标:

# Foo/CMakeLists.txt
option(FOO_BUILD_DOCS "Build doc target for Foo project" OFF)
# ...
if(DOXYGEN_FOUND AND FOO_BUILD_DOCS)
  add_custom_target(doc ...)
endif()

# Boo/CMakeLists.txt
option(BOO_BUILD_DOCS "Build doc target for Boo project" OFF)
# ...
if(DOXYGEN_FOUND AND BOO_BUILD_DOCS)
  add_custom_target(doc ...)
endif()
83qze16e

83qze16e2#

if (NOT (TARGET DOCUMENTATION))
    add_custom_target(DOCUMENTATION COMMENT "workaround for multi-dependencies project")
    add_dependencies(DOCUMENTATION DOCUMENTATION_${PROJECT_NAME})
endif()

相关问题