Visual Studio 代码 g++ 在调试时不能用 c++17 编译
Visual Studio Code g++ does not compile with c++17 when debugging
我想编译当前工作区文件夹中的活动文件。
使用 C++11 文件没有问题我只是点击 运行-> 开始调试并生成正确的 launch.json
和 tasks.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 编译的情况下执行的。我收到此错误:
Executing task: 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++ (海湾合作委员会) 10.2.0
VSCode 1.15.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"
}
]
}
我也遇到了和你一样的问题,不过我已经轻松解决了。您只需在 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"
},
我想编译当前工作区文件夹中的活动文件。
使用 C++11 文件没有问题我只是点击 运行-> 开始调试并生成正确的 launch.json
和 tasks.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 编译的情况下执行的。我收到此错误:
Executing task: 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++ (海湾合作委员会) 10.2.0
VSCode 1.15.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"
}
]
}
我也遇到了和你一样的问题,不过我已经轻松解决了。您只需在 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"
},