【问题标题】:How do I set up Visual Studio Code problemMatcher to match C++ errors?如何设置 Visual Studio Code problemMatcher 以匹配 C++ 错误?
【发布时间】:2017-02-02 01:33:01
【问题描述】:

我设置了在 Visual Studio Code 中构建代码的任务(类似于 How do I set up VSCode to compile C++ code?),但正则表达式与 g++ 的输出不匹配。有什么我做错了吗?这是在 MacOS Sierra 上。

我有一个名为 ItemAssignmentMatrix.cpp 的源文件,其中第 15 行有一个错误行。

thisisanerror;

我的 tasks.json 问题匹配器模式正则表达式如下所示:

{
    "version": "0.1.0",
    "command": "make",
    "isShellCommand": true,
    "showOutput": "always",
    "echoCommand": true,
    "suppressTaskName": true,
    "tasks" : [
        {
            "taskName": "clean",
            "args": ["-f" "${workspaceRoot}/Makefile" "clean"]
        },
        {
            "taskName": "build",
            "isBuildCommand": true,
            "args": ["-f" "${workspaceRoot}/Makefile"],
            // Use the standard less compilation problem matcher.
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

当我执行此任务时,我的 Makefile(使用 g++)输出如下错误行:

g++ -g -Wall -c -o obj/Darwin_x86_64/ItemAssignmentMatrix.obj src/ItemAssignmentMatrix.cpp
src/ItemAssignmentMatrix.cpp:15:1: error: C++ requires a type specifier for all declarations
thisisanerror;
^
1 error generated.
make: *** [obj/Darwin_x86_64/ItemAssignmentMatrix.obj] Error 1

所以任务正确地执行了 Makefile,但是问题匹配器不匹配正则表达式,所有的输出只是在输出窗口中显示为文本。

正则表达式似乎与https://code.visualstudio.com/docs/editor/tasks 中给出的相同。

我尝试在https://regex101.com 中输入正则表达式和输出,但似乎也找不到匹配项。

我认为这是正则表达式的问题,但我对正则表达式语法的理解不够好,无法在这一点上进行调试。

此表达式与输出不匹配是否有某些原因,或者我的问题匹配器是否有其他问题?提前感谢您提供的任何帮助。

【问题讨论】:

  • 乍一看,我在您的args 数组中发现了一个问题:"args": ["-f" "${workspaceRoot}/Makefile"]。参数之间必须有逗号。也就是说,我认为 RegEx 没有任何问题。我在浏览器控制台中对其进行了测试,它可以工作。

标签: windows visual-studio-code


【解决方案1】:

GCC 的输出有一个内置的问题匹配器。请尝试以下操作:

        "problemMatcher": "$gcc"

它默认为工作区根目录的相对路径。你可以改变它使用

        "problemMatcher": {
            "base": "$gcc",
            "fileLocation": ["absolute"]
        },

例如。

【讨论】:

  • 还值得将 -fmessage-length=0 选项传递给 clang,以防止其将错误消息包装在多行上,从而导致 vscode 问题面板和工具提示中的错误被截断。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2021-11-06
  • 2021-06-26
  • 2021-06-16
相关资源
最近更新 更多