编译具有相同目标的不同子项目时,CMP0002错误 [英] CMP0002 error when compiling different subproject with same target
问题描述
我有很多子文件夹
home|| -library1| -library2|| -libraryn
每个子文件夹都包含一个完整的库,该库可以自己编译(每个库都有一个不同的管理器).到目前为止,它可以正常工作,我使用脚本对其进行编译.
现在,我需要创建另一个库,该库取决于现有库.为此,我使用允许我编译所有库的 add_subdirectory
命令在主文件夹下创建了 CMakeLists.txt
.
我喜欢
cmake_minimum_required(版本2.8)add_subdirectory(library1)add_subdirectory(library2)...add_subdirectory(libraryn)
当我尝试执行 cmake
时,我得到了各种库的以下错误:
CMake错误:add_custom_target无法创建目标"doc",因为具有相同的名称已经存在.现有目标是自定义目标在源目录中创建"/path/to/libraryX".看有关更多详细信息,请参阅策略CMP0002的文档.
之所以会发生这种情况,是因为在每个库中,我们都会创建一个doc目标,以便编译该库本身的Doxygen文档.当库被一个一个地编译时,它可以很好地工作,但是对于主 CMakeLists.txt
来说,似乎我做不到.
#创建doxygen文档编译的文档目标.find_package(Doxygen)如果(DOXYGEN_FOUND)设置(Doxygen_Dir $ {CMAKE_BINARY_DIR}/export/$ {Library_Version}/doc)#复制图片文件夹文件(GLOB IMAGES_SRC图像/*")文件(COPY $ {IMAGES_SRC}目的地$ {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}/DoxyfileWORKING_DIRECTORY $ {CMAKE_CURRENT_BINARY_DIR}评论生成doxygen文档" VERBATIM)其他(DOXYGEN_FOUND)消息(状态必须安装Doxygen才能编译doc")endif(DOXYGEN_FOUND)
有没有一种方法可以立即编译这些项目而无需修改此目标?
如果您不想修改任何内容,因此可以将所有这些项目构建为子项目,则可以使用 option 命令来从构建中排除 doc
个目标:
#Foo/CMakeLists.txt选项(FOO_BUILD_DOCS为Foo项目构建文档目标"为OFF)#...如果(DOXYGEN_FOUND AND FOO_BUILD_DOCS)add_custom_target(doc ...)万一()#Boo/CMakeLists.txt选项(BOO_BUILD_DOCS为Boo项目构建文档目标"为OFF)#...如果(DOXYGEN_FOUND AND BOO_BUILD_DOCS)add_custom_target(doc ...)万一()
I've many sub-folders
home
|
|-library1
|-library2
|
|-libraryn
Every subfolder contains a complete library that can compile by itself (every library has a different mantainer). Until now it works correctly and I compile them using a script.
Now I need to create another library, that depends on existing one. In order to do so, I've created a CMakeLists.txt
under home folder, using add_subdirectory
command that allows me to compile all libraries.
I've something like
cmake_minimum_required (VERSION 2.8)
add_subdirectory(library1)
add_subdirectory(library2)
...
add_subdirectory(libraryn)
When I try to execute cmake
I obtain following error for various libraries:
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.
This happens because in every library we create a doc target in order to compile the Doxygen documentation of the library itself. It works fine when libraryes are compiled one by one, but with the master CMakeLists.txt
it seems that I cannot do it.
# 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)
Is there a way to compile these project at once without modify this target?
If you don't want to modify anything so you can build all this projects as subprojects, then you can use ExternalProject_Add to build and install dependencies.
option
Alternatively you can use option command to exclude doc
target from build:
# 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()
这篇关于编译具有相同目标的不同子项目时,CMP0002错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!