Macros 跨CmakeList的CMake宏 我有一个C++项目,目录结构如下: server/ code/ BASE/ Thread/ Log/ Memory/ Net/ cmake/ CMakeList.txt BASE/ CMakeList.txt Net/ CMakeList.txt
以下是/cmake/CMakeList.txt的一部分:Macros 跨CmakeList的CMake宏 我有一个C++项目,目录结构如下: server/ code/ BASE/ Thread/ Log/ Memory/ Net/ cmake/ CMakeList.txt BASE/ CMakeList.txt Net/ CMakeList.txt,macros,cmake,Macros,Cmake,以下是/cmake/CMakeList.txt的一部分: MACRO(SUBDIRLIST result curdir) FILE(GLOB children RELATIVE ${curdir} ${curdir}/*) SET(dirlist "") FOREACH(child ${children}) IF(IS_DIRECTORY ${curdir}/${child}) SET(dirlist ${dirlist} ${child}) ENDI
MACRO(SUBDIRLIST result curdir)
FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
SET(dirlist "")
FOREACH(child ${children})
IF(IS_DIRECTORY ${curdir}/${child})
SET(dirlist ${dirlist} ${child})
ENDIF()
ENDFOREACH()
SET(${result} ${dirlist})
ENDMACRO()
add_subdirectory(Base)
SET(SUBDIR, "")
SUBDIRLIST(SUBDIRS, ${BASE_SRC_DIR})
message("SUBDIRS : " ${SUBDIRS})
然后使用/cmake/Base/CMakeList.txt中的宏:
MACRO(SUBDIRLIST result curdir)
FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
SET(dirlist "")
FOREACH(child ${children})
IF(IS_DIRECTORY ${curdir}/${child})
SET(dirlist ${dirlist} ${child})
ENDIF()
ENDFOREACH()
SET(${result} ${dirlist})
ENDMACRO()
add_subdirectory(Base)
SET(SUBDIR, "")
SUBDIRLIST(SUBDIRS, ${BASE_SRC_DIR})
message("SUBDIRS : " ${SUBDIRS})
输出:
细分市场:
我通过在宏中输出它的值来检查${dirlist},我得到了预期的目录列表,但是当设置(${result}${dirlist})后出现消息(“result”${result})时,我无法得到预期的输出,我的CMakeLists.txt有什么问题吗?这里有几个小问题:
SET(dirlist“”)
可以是SET(dirlist)
。同样地,SET(SUBDIR,””
可以是SET(SUBDIRS)
(我猜“SUBDIR”是一个拼写错误,应该是“SUBDIRS”。另外,您不希望在SET
命令中使用逗号-可能是另一个拼写错误?)${result}
的内容,请使用消息(“result:${${result}}”)
,因为您不是每次都将${child}
追加到结果
。在您的示例中,${result}
是SUBDIRS
,因此${${result}
是${SUBDIRS}
子列表时,不要在参数之间使用逗号
SUBDIRS
的值时,在引号中包括${SUBDIRS}
,即消息(“SUBDIRS:${SUBDIRS}”)
,否则您将丢失分号分隔符