【发布时间】:2020-11-16 17:32:48
【问题描述】:
我想编译当前工作区文件夹中的活动文件。
使用 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 编译的情况下执行的。我收到此错误:
执行任务: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 <optional>在某处(当你不通过命令行编译时)? -
我认为它不使用tasks.json进行编译!
标签: c++ visual-studio-code g++