我正在编写一个游戏引擎,我有一个名为thirdparty
的目录,其中放置了我的引擎使用的所有外部库:
thirdparty/...
/zlib
/freetype2
/SDL2
/...
因此,引擎目录结构如下所示:
engine/actors
engine/memory_allocator
engine/renderer
engine/...
engine/thirdparty
engine/thirdparty/CMakeLists.txt
engine/CMakeLists.txt
engine/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)
engine/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.
有什么方法可以解决这个问题吗?也许,有一种方法可以通过添加任何前缀来重命名这些目标,以便它们永远不会发生冲突吗?
答案 0 :(得分:2)
这主要取决于您定位的生成器。对它们施加限制的解决方案是使用ALLOW_DUPLICATE_CUSTOM_TARGETS
。
从文档中可以看出:
允许创建重复的自定义目标。
它仅适用于Makefile生成器:
对于仅关注Makefile生成器且不希望支持Xcode或VS IDE生成器的项目,可以将此属性设置为true以允许重复的自定义目标。
否则,请考虑不直接打包项目的依赖项,并使用其他工具(如Find*
)将它们与目标系统上的正确路径一起使用。