问题:想要实现一个宏,将文件列表转换出相对路径列表
#所有绝对路径转相对路径 macro(CUSTOM_FK_ABSOLUTE_TO_RELATIVE root_dir absolutePaths relativePaths) set (extra_macro_args ${ARGN}) list(LENGTH extra_macro_args ext_param_length) # 获取动态扩展参数 if (${ext_param_length} GREATER 0) foreach(optional_arg ${extra_macro_args}) message ("extend arg: ${optional_arg}") endforeach() endif () message(222222222222 ${absolutePaths}) list(LENGTH ${absolutePaths} param_length) message(ssssssssssssss:${param_length}) #遄历目录和文件 foreach(sub ${absolutePaths}) message(000000000000000${sub}) file(RELATIVE_PATH relative_path ${root_dir} ${sub}) list(APPEND ${relativePaths} ${relative_path}) message(11111111111111${relative_path}) endforeach() endmacro()
问题来了:
CUSTOM_FK_RECURSION_DIR_H_AND_CPP(${PROJECT_SOURCE_DIR} H_FILES CPP_FILES) message(STATUS ${H_FILES}) CUSTOM_FK_ABSOLUTE_TO_RELATIVE(${PROJECT_SOURCE_DIR} ${H_FILES} RELATIVE_FILES)
在调用
CUSTOM_FK_ABSOLUTE_TO_RELATIVE
时,H_FILESl列表传参过去,在宏接收处只收到了列表中第一个元素
-- /cygdrive/f/Project/FK/common/FKComponent/FKPub/ComponentBase.h/cygdrive/f/Project/FK/common/FKComponent/FKPub/FKPub.h/cygdrive/f/Project/FK/common/FKComponent/FKPub/FKPub_global.h/cygdrive/f/Project/FK/common/FKComponent/FKPub/FKPunDef.h extend arg: /cygdrive/f/Project/FK/common/FKComponent/FKPub/FKPub_global.h extend arg: /cygdrive/f/Project/FK/common/FKComponent/FKPub/FKPunDef.h extend arg: RELATIVE_FILES 222222222222/cygdrive/f/Project/FK/common/FKComponent/FKPub/ComponentBase.h ssssssssssssss:0 000000000000000/cygdrive/f/Project/FK/common/FKComponent/FKPub/ComponentBase.h 11111111111111ComponentBase.h
在对函数和宏传参时,传参是列表时,直接使用列表名,如:H_FILES而不是使用${H_FILES},直接使用${H_FILES}时,函数和宏参数将会展开列表,覆盖参数,
如:
set (H_FILES a b c d)
针对上面函数:
CUSTOM_FK_ABSOLUTE_TO_RELATIVE(${PROJECT_SOURCE_DIR} ${H_FILES} RELATIVE_FILES)
实际RELATIVE_FILES参数变成了b,和我们想象有差异
解决办法:
CUSTOM_FK_ABSOLUTE_TO_RELATIVE(${PROJECT_SOURCE_DIR} H_FILES RELATIVE_FILES)
但使用此方法,会导致循环无法使用,将会报错
file RELATIVE_PATH must be passed a full path to the file: H_FILES
解决办法:
修改循环参数写法
foreach(sub IN LISTS ${H_FILES})
修改循环变量写法
foreach(sub ${${absolutePaths}})
还没有评论,来说两句吧...