I have a c++ project which directory structure like below:
server/
code/
BASE/
Thread/
Log/
Memory/
Net/
cmake/
CMakeList.txt
BASE/
CMakeList.txt
Net/
CMakeList.txt
here is part of /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})
ENDIF()
ENDFOREACH()
SET(${result} ${dirlist})
ENDMACRO()
add_subdirectory(Base)
then use macro in /cmake/Base/CMakeList.txt:
SET(SUBDIR, "")
SUBDIRLIST(SUBDIRS, ${BASE_SRC_DIR})
message("SUBDIRS : " ${SUBDIRS})
output:
SUBDIRS :
I check ${dirlist} by output it’s value in macro, I get directory list expected,but when message(“result ” ${result}) after SET(${result} ${dirlist}),I can not get output expected , what’s wrong with my CMakeLists.txt?
There are a couple of minor issues here:
SET(dirlist "")could be justSET(dirlist). Likewise,SET(SUBDIR, "")could be justSET(SUBDIRS)(I guess “SUBDIR” is a typo and should be “SUBDIRS”. Also you don’t want the comma in thesetcommand – probably another typo?)${result}in the macro, usemessage("result: ${${result}}"), since you’re not appending${child}toresulteach time, but to${result}. In your example${result}isSUBDIRS, so${${result}}is${SUBDIRS}.SUBDIRLIST, don’t use a comma between the arguments.SUBDIRS, include${SUBDIRS}in the quotes, i.e.message("SUBDIRS: ${SUBDIRS}")or you’ll lose the semi-colon separators.Other than those, your macro seems fine.