【问题标题】:Visual Studio Code g++ does not compile with c++17 when debugging调试时 Visual Studio Code g++ 不能用 c++17 编译
【发布时间】:2020-11-16 17:32:48
【问题描述】:

我想编译当前工作区文件夹中的活动文件。

使用 C++11 文件没有问题,我只需点击运行->开始调试,就会生成正确的 launch.jsontasks.json

但是对于包含 C++17 代码的文件,我遇到了麻烦。

我认为在 tasks.json 中添加标志 -std=c++17 可以在使用 C++17 进行调试时编译,但它不起作用。

这里是配置文件:

launch.jsons

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Generated task by Debugger"
        }
    ],
    "version": "2.0.0"
}

据我了解,应该会发生以下情况:

当我首先启动调试器时,应该使用以下命令编译文件:

"preLaunchTask": "C/C++: g++ build active file",

launch.json.

tasks.json 中,此任务定义为使用 C++17 标志调用 g++:

            "command": "/usr/bin/g++",
            "args": [
                "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],

当运行它时,看起来任务是在没有 C++17 编译的情况下执行的。我收到此错误:

执行任务:C/C++:g++ build active file

Starting build...
Build finished with error:

/...Solution2.cpp:48:10: error: ‘optional’ in namespace ‘std’ does not name a template type
   48 |     std::optional<Stack> getLastStack() const;
      |          ^~~~~~~~

为了验证编译器的工作原理,只需从命令行编译文件

g++ -std=c++17 Solution2.cpp

然后从命令行运行它:

./a.out

我的系统是 Manjaro Linux。

g++ (GCC) 10.2.0

VSCode 1.15.1

【问题讨论】:

  • 你的编译器支持c++17吗?
  • 说实话,我也有同样的期望。你能得到IDE生成的完整的编译器命令行吗?如果没有比解决此类问题更好的理由,他们通常会将其存放在某个地方。确定-std=c++17 正在进入编译器。
  • 在命令行上使用 g++ -std=++17 可以正常编译。如何获得完整的命令行?
  • 真是个愚蠢的问题,但你确定#include &lt;optional&gt; 在某处(当你不通过命令行编译时)?
  • 我认为它不使用tasks.json进行编译!

标签: c++ visual-studio-code g++


【解决方案1】:

看起来有一个名为“C/C++:g++ build active file”的“内置”任务,所以即使它没有在tasks.json 中定义但在launch.json 中使用,它仍然会启动。 .. 使用某些默认配置,您当然无法更改。尝试从tasks.json 中删除此任务,以查明这是真的。

这个内置任务似乎也比您定义的任务具有更高的优先级。这可能是 VSC 中的一个错误,但更改任务名称的简单解决方法似乎可以解决问题。

只需将您的任务重命名为默认名称即可。即你的tasks.json 看起来像这样(注意label 键是如何定义的):

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "Totally custom debug task name",
            "command": "/usr/bin/g++",
            "args": [
                "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Generated task by Debugger"
        }
    ],
    "version": "2.0.0"
}

还有你的launch.json - 像这样(注意preLaunchTask 是如何定义的):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Totally custom debug task name",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

【讨论】:

  • 这完全解决了我的问题。我没有想到有一些同名的默认任务隐藏在任务文件中。我想知道这个默认任务是否可以编辑。
  • 就像我说的它可能是内置的,而您遇到的这种行为只是一个错误。与此类似:github.com/microsoft/vscode/issues/111048 - 我继续评论了您遇到的问题,因为它看起来具有相同的来源,所以希望开发人员能够处理它。
【解决方案2】:

我和你有同样的问题,但我已经很容易解决了。您只需在tasks.json 文件中的"-g", 之后添加"-std=c++17",,无需执行任何操作。 这是我的tasks.json 文件的内容:

    {
        "type": "shell",
        "label": "C/C++: g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-g",
            "-std=c++17",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build"
    },

这是我的launch.json 文件的内容:

{
        "name": "Linux C/C++ Debug",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "C/C++: g++ build active file",
        "miDebuggerPath": "/usr/bin/gdb"
    },

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 1970-01-01
    • 2019-06-28
    • 2020-03-15
    • 2019-07-24
    • 2017-05-09
    • 2020-02-04
    • 1970-01-01
    相关资源
    最近更新 更多