How to split strings across multiple lines in CMake?

后端 未结 7 1555
花落未央
花落未央 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:23

    CMake 3.0 and newer

    Use the string(CONCAT) command:

    set(MYPROJ_VERSION_MAJOR "1")
    set(MYPROJ_VERSION_MINOR "0")
    set(MYPROJ_VERSION_PATCH "0")
    set(MYPROJ_VERSION_EXTRA "rc1")
    string(CONCAT MYPROJ_VERSION "${MYPROJ_VERSION_MAJOR}"
                                 ".${MYPROJ_VERSION_MINOR}"
                                 ".${MYPROJ_VERSION_PATCH}"
                                 "-${MYPROJ_VERSION_EXTRA}")
    

    Although CMake 3.0 and newer support line continuation of quoted arguments, you cannot indent the second or subsequent lines without getting the indentation whitespace included in your string.

    CMake 2.8 and older

    You can use a list. Each element of the list can be put on a new line:

    set(MYPROJ_VERSION_MAJOR "1")
    set(MYPROJ_VERSION_MINOR "0")
    set(MYPROJ_VERSION_PATCH "0")
    set(MYPROJ_VERSION_EXTRA "rc1")
    set(MYPROJ_VERSION_LIST "${MYPROJ_VERSION_MAJOR}"
                            ".${MYPROJ_VERSION_MINOR}"
                            ".${MYPROJ_VERSION_PATCH}"
                            "-${MYPROJ_VERSION_EXTRA}")
    

    A list used without quotes is concatenated without white-space:

    message(STATUS "Version: " ${MYPROJ_VERSION_LIST})
    -- Version: 1.0.0-rc1
    

    If you really need a string, you can convert the list to a string first:

    string(REPLACE ";" "" MYPROJ_VERSION "${MYPROJ_VERSION_LIST}")
    message(STATUS "Version: ${MYPROJ_VERSION}")
    -- Version: 1.0.0-rc1
    

    Any semicolons in your original strings will be seen as list element separators, and removed. They must be escaped:

    set(MY_LIST "Hello World "
                "with a \;semicolon")
    

提交回复
热议问题