How to split strings across multiple lines in CMake?

后端 未结 7 1558
花落未央
花落未央 2021-01-06 19:03

I usually have a policy in my project, to never create lines in text files that exceed a line length of 80, so they are easily editable in all kinds of editors (you know the

7条回答
  •  臣服心动
    2021-01-06 19:41

    There is no way to split a string literal across multiple lines in CMakeLists.txt files or in CMake scripts. If you include a newline within a string, there will be a literal newline in the string itself.

    # Don't do this, it won't work, MYPROJ_VERSION will contain newline characters:
    set(MYPROJ_VERSION "${VERSION_MAJOR}.
      ${VERSION_MINOR}.${VERSION_PATCH}-
      ${VERSION_EXTRA}")
    

    However, CMake uses whitespace to separate arguments, so you can change a space that's an argument separator into a newline anywhere you like, without changing the behavior.

    You could re-phrase this longer line:

    set(MYPROJ_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_EXTRA}")
    

    as these two shorter lines:

    set(MYPROJ_VERSION
      "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_EXTRA}")
    

    They are entirely equivalent.

提交回复
热议问题