gpt4 book ai didi

CMake - 检查是否存在更高级别的目录

转载 作者:行者123 更新时间:2023-12-03 16:05:06 31 4
gpt4 key购买 nike

我是 CMake 的新手,我尝试在我的根 CMakeLists.txt 中执行此操作:

set(HAVE_MY_SDK   OFF)

if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}../Libs/A")
if(EXISTS "${PROJECT_SOURCE_DIR}../Libs/A/CMakeLists.txt")
set (HAVE_MY_SDK ON)
endif()
endif()

目前,CMake 只是避开指令,留下 HAVE_MY_SDKOFF地位。是否可以使用 CMake 检查更高级别的目录?或者也许用间接方法来做

最佳答案

我想你只是缺少一个 /之后 ${PROJECT_SOURCE_DIR} .

出于完整性,这里是我为此使用的代码(注意 if (EXISTS ...) 需要完整路径):

get_filename_component(_fullpath "${_dir}" REALPATH)
if (EXISTS "${_fullpath}" AND EXISTS "${_fullpath}/CMakeLists.txt")
...

在我的扩展 add_subdirectory() 里面版本(包括“添加一次守卫”):
function(my_add_subdirectory _dir)
get_filename_component(_fullpath "${_dir}" REALPATH)
if (EXISTS "${_fullpath}" AND EXISTS "${_fullpath}/CMakeLists.txt")
get_property(_included_dirs GLOBAL PROPERTY GlobalAddSubdirectoryOnceIncluded)
list(FIND _included_dirs "${_fullpath}" _used_index)
if (${_used_index} EQUAL -1)
set_property(GLOBAL APPEND PROPERTY GlobalAddSubdirectoryOnceIncluded "${_fullpath}")
add_subdirectory(${_dir} ${ARGN})
endif()
else()
message(WARNING "my_add_subdirectory: Can't find ${_fullpath}/CMakeLists.txt")
endif()
endfunction(my_add_subdirectory)

关于CMake - 检查是否存在更高级别的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47674915/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com