我想使用Mac的clang当前不支持的某些C ++ 17功能,所以我使用
brew install gcc --HEAD
以安装g ++ 10.0.1版本。通过直接调用
,代码在终端中运行良好g++-HEAD -std=c++17 test.cpp
我还在bash中创建了一个链接ln -s g++-HEAD g++
,并在alias g++='g++ -std=c++17'
中添加了别名.bash_profile
,以便
g++ test.cpp
将执行相同的工作。
我想在Visual Studio Code-Mac版本中运行C ++代码。在安装了Microsoft的C / C ++扩展和Code Runner扩展之后,我在VSCode中设置了settings.json
文件以包含编译器参数:
{
"C_Cpp.default.cppStandard": "c++17",
"C_Cpp.default.compilerPath": "/usr/bin/g++",
"C_Cpp.default.intelliSenseMode": "gcc-x64",
"C_Cpp.default.compilerArgs": [
"-std=c++17"
]
}
然后,我尝试运行相同的代码。但是,我得到了警告:
[Running] cd "/some directory/" && g++ test.cpp -o test && "/some directory/"test
warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17'
显然,这意味着在VSCode中运行的g ++编译器不是我手动设置的别名。更有趣的是,如果我直接在VSCode TERMINAL中运行,则我以前的代码
g++ test.cpp -o test
作品。
我对VSCode中的设置感到困惑:为什么运行程序不使用与VSCode自己的终端中使用的相同的g++
编译器参数?另外,如何修改VSCode中的settings.json
文件或其他文件,以便可以正确添加-std=c++17
参数?
这是我解决这一困境的方法。我将其与我的项目Makefile链接。
[首先,如果您自己使用一个Makefile来使用GCC的话……至少可以说是有争议的,您需要构建一个Makefile。但我认为这符合本示例的目的。您可能知道,在当前目录中运行“ make”而不是g ++将解析Makefile并运行相应的命令。但就您而言,您的Makefile可能类似于:
#You're gonna wanna make it think that your executable doesnt exist, otherwise,
#because the executable exists, make will assume its the most recent build.
.PHONY: debug
#using g++ and the flags inline like this, is generally seen as bad practice, it might be worth
#looking into using Makefiles to make this more acceptable, but this will get you started.
debug:
g++ -g -o debug main.cpp -std=c++17
clean:
rm debug
#if you copy this exactly, make you you replace the spaces with proper tab
#characters otherwise it will error out.
多汁部分在VS代码中;它具有称为任务的强大功能。任务是它们自己的特殊兔子孔,但说白了,您可以将一个任务添加到工作区中生成的task.json文件中的“任务”数组中,如果您不熟悉它的外观,这是任务的语法:
{
"label": "[name of task]",
"type": "[type of task, usually its a shell command]",
"command": "[the actual command to call]"
}
任务可以提供更多功能,但是对于构建工具而言,这正是您所需要的,对我来说,生成的文件看起来像这样:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build_debug",
"type": "shell",
"command": "make"
},
{
"label": "clean",
"type": "shell",
"command": "make clean"
},
{
"label": "build",
"dependsOn": [
"clean",
"build_debug"
],
"problemMatcher": [
"$gcc"
]
}
]
}
为什么我们需要进行最终的构建电话?因为您的launch.json对象可以接受一个“ preLaunchTask”,它将在调试之前自动调用。您可以打入最后的构建调用,它将进行编译,调试,然后运行您的应用程序。它甚至会将GDB断点和内存跟踪集成到工作区中。我的launch.json看起来像这样:
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/runnable",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}
很抱歉重播,希望对您有所帮助:)