Visual Studio Code g++ 在调试时不能用 c++17 编译

San*_*912 0 c++ g++ visual-studio-code

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

用C ++ 11的文件没有问题,我只是打运行- >开始调试,右launch.jsontasks.json生成。

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

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

这里的配置文件:

启动文件

{
    // 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"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

任务文件

{
    "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"
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,应该发生以下情况:

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

"preLaunchTask": "C/C++: g++ build active file",
Run Code Online (Sandbox Code Playgroud)

launch.json

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

            "command": "/usr/bin/g++",
            "args": [
                "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
Run Code Online (Sandbox Code Playgroud)

仍然在运行时看起来任务是在没有 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;
      |          ^~~~~~~~
Run Code Online (Sandbox Code Playgroud)

要验证编译器是否工作,只需从命令行编译文件

g++ -std=c++17 Solution2.cpp
Run Code Online (Sandbox Code Playgroud)

并从命令行运行它:

./a.out
Run Code Online (Sandbox Code Playgroud)

我的系统是 Manjaro Linux。

g++ (GCC) 10.2.0

VSCode 1.15.1

Poo*_*Fan 6

看起来有一个名为“C/C++: g++ build active file”的“内置”任务,所以即使它没有在中定义tasks.json但在中使用launch.json,它仍然会被启动......使用一些默认配置你可以' t 改变,当然。尝试从中删除此任务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"
}
Run Code Online (Sandbox Code Playgroud)

而你的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"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)