加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 子目录之间的CMake自定义目标名称冲突

发布时间:2020-12-16 07:21:58 所属栏目:百科 来源:网络整理
导读:我正在编写一个游戏引擎,我有一个名为thirdparty的目录,其中放置了我的引擎使用的所有外部库: thirdparty/... /zlib /freetype2 /SDL2 /... 因此,引擎目录结构如下所示: engine/actorsengine/memory_allocatorengine/rendererengine/...engine/thirdpartye
我正在编写一个游戏引擎,我有一个名为thirdparty的目录,其中放置了我的引擎使用的所有外部库:

thirdparty/...
          /zlib
          /freetype2
          /SDL2
          /...

因此,引擎目录结构如下所示:

engine/actors
engine/memory_allocator
engine/renderer
engine/...
engine/thirdparty
engine/thirdparty/CMakeLists.txt
engine/CMakeLists.txt

引擎/ CMakeLists.txt:

cmake_minimum_required (VERSION 3.2)

project(Engine)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fvisibility=hidden -fvisibility-inlines-hidden -Wall -Werror")
# Include build directory to be able to include generated files:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
file(GLOB GLAD_SOURCES thirdparty/glad/src/glad.c)
file(GLOB ACTOR_SOURCES actors/*.cpp)
file(GLOB LOGIC_SOURCES logic/*.cpp)
file(GLOB UI_SOURCES ui/*.cpp)
set(SOURCES ${GLAD_SOURCES}
            ${ACTOR_SOURCES}
            ${LOGIC_SOURCES}
            ${UI_SOURCES})
add_library(engine SHARED ${SOURCES})

include_directories(thirdparty/glm/)
include_directories(thirdparty/glad/include)
include_directories(thirdparty/tinyxml2/)
include_directories(thirdparty/zlib/)
include_directories(thirdparty/sdl2/SDL2-2.0.5/)
include_directories(./)
add_subdirectory(../../thirdparty thirdparty)

target_link_libraries(engine SDL2)
target_link_libraries(engine tinyxml2)
target_link_libraries(engine zlib)

引擎/ thirdparty / CMakeLists.txt:

set(BUILD_CPU_DEMOS OFF CACHE BOOL "" FORCE)
set(BUILD_DEMOS OFF CACHE BOOL "" FORCE)
set(BUILD_EXTRAS OFF CACHE BOOL "" FORCE)
set(BUILD_BULLET2_DEMOS OFF CACHE BOOL "" FORCE)
set(BUILD_UNIT_TESTS OFF CACHE BOOL "" FORCE)
set(FREETYPE_NO_DIST OFF CACHE BOOL "" FORCE)

set(OLD_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "")
# add_subdirectory(bullet3)

set(CMAKE_CXX_FLAGS "${OLD_CXX_FLAGS}")
add_subdirectory(freetype2)

set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
add_subdirectory(tinyxml2)

add_subdirectory(sdl2/SDL2-2.0.5/)

add_subdirectory(zlib)

因此,主引擎CMakeLists.txt位于引擎源的根目录中,还有另一个CMakeLists.txt用于在引擎根目录的thirdparty子目录中构建第三方库.

问题是第三方中的一些子目录,例如SDL2和zlib包含相同的目标名称,通过add_custom_target添加,因此由于此命名冲突,CMake无法生成Makefile:

zlib的CMakeLists.txt(最后):

# uninstall target
if(NOT TARGET uninstall)
  configure_file(
      "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
      "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
      IMMEDIATE @ONLY)

  add_custom_target(uninstall
      COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()

SDL2的CMakeLists.txt:

##### Uninstall target #####

configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)

add_custom_target(uninstall
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)

所以最后的错误是:

CMake Error at /home/user/engine/thirdparty/sdl2/SDL2-2.0.5/CMakeLists.txt:1624 (add_custom_target):
  add_custom_target cannot create target "uninstall" because another target
  with the same name already exists.  The existing target is a custom target
  created in source directory
  "/home/user/engine/thirdparty/zlib".  See
  documentation for policy CMP0002 for more details.

有没有什么办法解决这一问题?也许,有一种方法可以通过添加任何前缀来重命名这些目标,以便它们永远不会发生冲突?

解决方法

它主要取决于您要定位的生成器.对它们施加限制的解决方案是使用 ALLOW_DUPLICATE_CUSTOM_TARGETS.

从文档中可以看出:

Allow duplicate custom targets to be created.

它只适用于Makefile生成器:

For projects that care only about Makefile generators and do not wish to support Xcode or VS IDE generators,one may set this property to true to allow duplicate custom targets.

否则,请考虑不直接打包项目的依赖项,并使用其他工具(如Find *)将它们与目标系统上的正确路径一起使用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读