CMake 字符串替换删除分号
CMake string replace removes semi-colon
我有一个包含多个占位符的模板 cpp 文件。
摘录:
// WARNING! this file is autogenerated, do not edit manually
QString appName()
{
return "APP_NAME_VALUE";
}
Cmake 将读取此文件,填充占位符并将其写回影子构建目录以进行编译
set(APP_NAME "real application name")
file(READ ${CMAKE_SOURCE_DIR}/templates/app-info.cpp APP_INFO)
string(REPLACE "APP_NAME_VALUE" ${APP_NAME} APP_INFO ${APP_INFO})
# other tag replacements
file(WRITE "${CMAKE_BINARY_DIR}/src/app-info.cpp" ${APP_INFO})
但每次我 运行 cmake 时,它似乎从文件内容中删除了分号。
// WARNING! this file is autogenerated, do not edit manually
QString appName()
{
return "real application name"
}
这是预期的行为吗?
我能做些什么来解决这个问题?
来自https://discourse.cmake.org/t/what-is-the-best-way-to-search-and-replace-strings-in-a-file/1879
In my CMake script, I need to modify other source files by searching and replacing specified strings. In my case, the configure_file 2 command is not a solution because I have no control over the input file. Previously I used the file and string commands in the following way -
file(READ header.h FILE_CONTENTS)
string(REPLACE "old text" "new text" FILE_CONTENTS ${FILE_CONTENTS})
file(WRITE header.h ${FILE_CONTENTS})
However this technique appears to strip out semi-colons from the input file.
答案:在两个命令中用引号将 ${FILE_CONTENTS} 括起来
解释:
It comes from a CMake’s list syntax, which is ;-separated, the fact that arguments passed to CMake commands are basically mashed together into a list, and that those commands take unbounded number of inputs. So you end up with one longer list.
Quoting prevents the semicolons in the expansion from being treated as list-element-separators.
考虑:
WRITE;header.h;x;y;z
对
WRITE;header.h;"x;y;z"
因此在您的情况下,它将显示为:
set(APP_NAME "real application name")
file(READ ${CMAKE_SOURCE_DIR}/templates/app-info.cpp APP_INFO)
string(REPLACE "APP_NAME_VALUE" ${APP_NAME} APP_INFO "${APP_INFO}")
# other tag replacements
file(WRITE "${CMAKE_BINARY_DIR}/src/app-info.cpp" "${APP_INFO}")
我有一个包含多个占位符的模板 cpp 文件。 摘录:
// WARNING! this file is autogenerated, do not edit manually
QString appName()
{
return "APP_NAME_VALUE";
}
Cmake 将读取此文件,填充占位符并将其写回影子构建目录以进行编译
set(APP_NAME "real application name")
file(READ ${CMAKE_SOURCE_DIR}/templates/app-info.cpp APP_INFO)
string(REPLACE "APP_NAME_VALUE" ${APP_NAME} APP_INFO ${APP_INFO})
# other tag replacements
file(WRITE "${CMAKE_BINARY_DIR}/src/app-info.cpp" ${APP_INFO})
但每次我 运行 cmake 时,它似乎从文件内容中删除了分号。
// WARNING! this file is autogenerated, do not edit manually
QString appName()
{
return "real application name"
}
这是预期的行为吗? 我能做些什么来解决这个问题?
来自https://discourse.cmake.org/t/what-is-the-best-way-to-search-and-replace-strings-in-a-file/1879
In my CMake script, I need to modify other source files by searching and replacing specified strings. In my case, the configure_file 2 command is not a solution because I have no control over the input file. Previously I used the file and string commands in the following way -
file(READ header.h FILE_CONTENTS)
string(REPLACE "old text" "new text" FILE_CONTENTS ${FILE_CONTENTS})
file(WRITE header.h ${FILE_CONTENTS})
However this technique appears to strip out semi-colons from the input file.
答案:在两个命令中用引号将 ${FILE_CONTENTS} 括起来
解释:
It comes from a CMake’s list syntax, which is ;-separated, the fact that arguments passed to CMake commands are basically mashed together into a list, and that those commands take unbounded number of inputs. So you end up with one longer list.
Quoting prevents the semicolons in the expansion from being treated as list-element-separators.
考虑:
WRITE;header.h;x;y;z
对
WRITE;header.h;"x;y;z"
因此在您的情况下,它将显示为:
set(APP_NAME "real application name")
file(READ ${CMAKE_SOURCE_DIR}/templates/app-info.cpp APP_INFO)
string(REPLACE "APP_NAME_VALUE" ${APP_NAME} APP_INFO "${APP_INFO}")
# other tag replacements
file(WRITE "${CMAKE_BINARY_DIR}/src/app-info.cpp" "${APP_INFO}")