在 CMake 中加入列表的最佳/最短方法
将 CMake 中的列表连接到字符串中的最佳方法是什么?
通过加入,我的意思是将 SET(somelist "a" "b" "c\;c") 转换为 "a:b:c;c",其中胶水字符串 (":") 是可选的。下面的代码可以工作,但它真的很长,有更好的方法吗?
FUNCTION(JOIN LISTNAME GLUE OUTPUT)
SET(_TMP_STR "")
FOREACH(VAL ${${LISTNAME}})
SET(_TMP_STR "${_TMP_STR}${GLUE}${VAL}")
ENDFOREACH(VAL ${${LISTNAME}})
STRING(LENGTH "${GLUE}" GLUE_LEN)
STRING(LENGTH "${_TMP_STR}" OUT_LEN)
MATH(EXPR OUT_LEN ${OUT_LEN}-${GLUE_LEN})
STRING(SUBSTRING "${_TMP_STR}" ${GLUE_LEN} ${OUT_LEN} _TMP_STR)
SET(${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
ENDFUNCTION()
#USAGE:
SET(somelist "a" "b" "c\;c")
JOIN(somelist ":" output)
MESSAGE("${output}") # will output "a:b:c;c"
不幸的是,使用 STRING(REPLACE ...) 不起作用:
function(JOINSTRREPLACE VALUES GLUE OUTPUT)
string (REPLACE ";" "${GLUE}" _TMP_STR "${VALUES}")
set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
endfunction()
JOINSTRREPLACE("${somelist}" ":" output)
MESSAGE(${output}) # will output "a:b:c\:c"
What is the best way to join a list in CMake into a string?
By joining I mean convert SET(somelist "a" "b" "c\;c") to "a:b:c;c" where the glue string (":") is choosable. The following code works but it is REALLY long, is there a better way?
FUNCTION(JOIN LISTNAME GLUE OUTPUT)
SET(_TMP_STR "")
FOREACH(VAL ${${LISTNAME}})
SET(_TMP_STR "${_TMP_STR}${GLUE}${VAL}")
ENDFOREACH(VAL ${${LISTNAME}})
STRING(LENGTH "${GLUE}" GLUE_LEN)
STRING(LENGTH "${_TMP_STR}" OUT_LEN)
MATH(EXPR OUT_LEN ${OUT_LEN}-${GLUE_LEN})
STRING(SUBSTRING "${_TMP_STR}" ${GLUE_LEN} ${OUT_LEN} _TMP_STR)
SET(${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
ENDFUNCTION()
#USAGE:
SET(somelist "a" "b" "c\;c")
JOIN(somelist ":" output)
MESSAGE("${output}") # will output "a:b:c;c"
Unfortunately using STRING(REPLACE ...) does not work:
function(JOINSTRREPLACE VALUES GLUE OUTPUT)
string (REPLACE ";" "${GLUE}" _TMP_STR "${VALUES}")
set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
endfunction()
JOINSTRREPLACE("${somelist}" ":" output)
MESSAGE(${output}) # will output "a:b:c\:c"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常这个任务可以通过简单的
string REPLACE
命令来解决。您可以在 cmake 附带的脚本中找到许多此类替换。但如果您确实需要关心值中的分号,那么您可以使用以下代码:Usually this task is solved with simple
string REPLACE
command. You can find a number of such replaces in scripts coming with cmake. But if you really need to care about semicolons inside you values, then you can use the following code:CMake 3.12 准备最终添加此上游:
https://github.com/Kitware/CMake/commit/a58158727be4585f9fd71449e9cc9e801c59a009
https://github.com/Kitware/CMake/commit/689eeb67cb87a9ed1d91a4971806488d00e68f42
CMake 3.12 is poised to finally add this upstream:
https://github.com/Kitware/CMake/commit/a58158727be4585f9fd71449e9cc9e801c59a009
https://github.com/Kitware/CMake/commit/689eeb67cb87a9ed1d91a4971806488d00e68f42
您可以使用 string REPLACE 函数:
You can use the string REPLACE function:
当您不使用分号或可以(并且想要)转义分号时,上面的答案是可以的。
我不喜欢转义分号,所以我写了下面的函数:
优点是列表可以为空,并且不必在变量中(但可以写在调用的地方)。
用法是:
The answers above are ok, when you do not use semicolons or when you can (and want to) escape them.
I prefer not to escape semicolons, so I wrote the following function:
The advantage is that the list can be empty and it doesn't have to be in a variable (but may be written in the place of invocation).
The usage is: