C++ Visual Studio代码g++;不使用c++;17调试时
我想编译当前工作区文件夹中的活动文件 对于C++11文件,我只需点击run->start debugging,就可以生成正确的C++ Visual Studio代码g++;不使用c++;17调试时,c++,visual-studio-code,g++,C++,Visual Studio Code,G++,我想编译当前工作区文件夹中的活动文件 对于C++11文件,我只需点击run->start debugging,就可以生成正确的launch.json和tasks.json 然而,对于一个包含C++17代码的文件,我遇到了麻烦 我认为在任务中添加标志-std=c++17。json将在使用c++17进行调试时编译,但它不起作用 以下是配置文件: launch.jsons { // Use IntelliSense to learn about possible attributes.
launch.json
和tasks.json
然而,对于一个包含C++17代码的文件,我遇到了麻烦
我认为在任务中添加标志-std=c++17
。json
将在使用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++构建活动文件看起来有一个名为“C/C++:g++构建活动文件”的“内置”任务,因此即使它没有在tasks.json
中定义,但在launch.json
中使用,它仍然会被启动。。。当然,对于某些默认配置,您无法更改。请尝试从tasks.json
中删除此任务,以确定是否正确
这个内置任务似乎比您定义的任务具有更高的优先级。这可能是VSC中的一个错误,但简单的解决方法是更改任务名称,似乎可以完成这项工作
只需将任务重命名为其他默认名称即可。Ie您的tasks.json
如下所示(注意label
key是如何定义的):
您的launch.json
-如下所示(注意preLaunchTask
是如何定义的):
我和你有同样的问题,但我很容易就解决了。您只需在
tasks.json
文件中的“-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"
},
{
"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"
},
这是我的launch.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"
},
{
"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++17吗?老实说,我也希望如此。您能获得IDE生成的完整编译器命令行吗?他们通常把它藏在某个地方,如果没有更好的理由来解决像这样的问题的话。请确保
-std=c++17
已发送到编译器。在命令行上,它只需使用g++-std=++17即可编译。我怎样才能获得完整的命令行?这真是个愚蠢的问题,但你确定#include
在那里的某个地方(当你不是通过命令行编译时)?我认为它不使用tasks.json进行编译!这完全解决了我的问题。我没有想到在任务文件中隐藏了一些同名的默认任务。我想知道这个默认任务是否可以编辑。就像我说的,它可能是内置的,而您遇到的这种行为只是一个bug。与此类似:-我继续对您遇到的问题发表了评论,因为它看起来有相同的来源,所以希望开发人员能够处理它。
{
"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"
}
]
}
{
"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"
},
{
"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"
},