-1

我在尝试打印带有新行的字符串时遇到了问题CMakeLists.txt

预期输出:

{file}:{line}:{column}
  Message: {message}
    Check: CWE-{cwe}
     Type: {severity} -> {id}
     Line: {line}:{column}
    Stack: {callstack}
{code}

如果我尝试放置新的换行符 ( \n),则会返回错误:

set(CPPCHECK_TEMPLATE "")
string(APPEND CPPCHECK_TEMPLATE "{file}:{line}:{column}\n")
string(APPEND CPPCHECK_TEMPLATE "  Message: {message}\n")
string(APPEND CPPCHECK_TEMPLATE "    Check: CWE-{cwe}\n")
string(APPEND CPPCHECK_TEMPLATE "     Type: {severity} -> {id}\n")
string(APPEND CPPCHECK_TEMPLATE "     Line: {line}:{column}\n")
string(APPEND CPPCHECK_TEMPLATE "    Stack: {callstack}\n")
string(APPEND CPPCHECK_TEMPLATE "{code}\n")

执行:

add_custom_command(
        COMMENT "Scanning: CPP Check\n"
        TARGET ${PROJECT_NAME}
        PRE_BUILD
        COMMAND cppcheck
        "--template=${CPPCHECK_TEMPLATE}"
        VERBATIM
)

错误:

/home/wolf/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/211.7142.21/bin/cmake/linux/bin/cmake --build /home/wolf/workspace/projects/bose-connect-app-linux/src/cmake-build-debug --target clean -- -j 6
CMakeFiles/bose-connect-app-linux.dir/build.make:157: *** target pattern contains no '%'.  Stop.
make[1]: *** [CMakeFiles/Makefile2:113: CMakeFiles/bose-connect-app-linux.dir/clean] Error 2
make: *** [Makefile:109: clean] Error 2
4

1 回答 1

0

我尝试了很多可能性,但没有得到任何解决方案。我发现如果反斜杠\被转义,那么它会识别新行(\\n):

set(CPPCHECK_TEMPLATE "")
string(APPEND CPPCHECK_TEMPLATE "{file}:{line}:{column}\\n")
string(APPEND CPPCHECK_TEMPLATE "  Message: {message}\\n")
string(APPEND CPPCHECK_TEMPLATE "    Check: CWE-{cwe}\\n")
string(APPEND CPPCHECK_TEMPLATE "     Type: {severity} -> {id}\\n")
string(APPEND CPPCHECK_TEMPLATE "     Line: {line}:{column}\\n")
string(APPEND CPPCHECK_TEMPLATE "    Stack: {callstack}\\n")
string(APPEND CPPCHECK_TEMPLATE "{code}\\n")

CMakeLists.txt此外,它适用于例如注释 ( )中的任何字符串COMMENT "Scanning: CPP Check\n\n"

add_custom_command(
        COMMENT "Scanning: CPP Check\n\n"
        TARGET ${PROJECT_NAME}
        PRE_BUILD
        COMMAND cppcheck
        "--language=c"
        "--std=c11"
        "--platform=unix64"
        "--template=${CPPCHECK_TEMPLATE}"
        "--project=${CMAKE_BINARY_DIR}/compile_commands.json"
        "--error-exitcode=1"
        VERBATIM)

注释中没有新行的输出:

Scanning: CPP Check
Checking /home/wolf/workspace/projects/bose-connect-app-linux/src/main.c ...

注释中带有新行的输出:

Scanning: CPP Check

Checking /home/wolf/workspace/projects/bose-connect-app-linux/src/main.c ...
于 2021-05-24T15:42:04.440 回答