dxf*_*ldu 5 c++ parameter-passing visual-studio-code
我想使用 Mac 的 clang 目前不支持的一些 C++17 功能,所以我使用
brew install gcc --HEAD
Run Code Online (Sandbox Code Playgroud)
安装 g++ 10.0.1 版本。通过直接调用,代码在终端中运行良好
g++-HEAD -std=c++17 test.cpp
Run Code Online (Sandbox Code Playgroud)
我还在 bash 中创建了一个链接ln -s g++-HEAD g++
,并alias g++='g++ -std=c++17'
在 中添加了一个别名.bash_profile
,这样
g++ test.cpp
Run Code Online (Sandbox Code Playgroud)
会做同样的工作。
我想在 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"
]
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试运行相同的代码。但是,我收到警告:
[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'
Run Code Online (Sandbox Code Playgroud)
显然,这意味着 VSCode 中运行的 g++ 编译器不是我手动设置的别名。更有趣的是,如果我直接在 VSCode TERMINAL 中运行,我之前的代码
g++ test.cpp -o test
Run Code Online (Sandbox Code Playgroud)
作品。
我对 VSCode 中的设置感到困惑:为什么运行程序不使用g++
与 VSCode 自己的终端中使用的相同的编译器参数?另外,我应该如何修改settings.json
VSCode 中的文件或其他一些文件,以便我可以正确添加-std=c++17
参数?
假设您使用 C/C++ 扩展,创建一个task.json
文件,该文件将允许您更改编译器路径、包含路径、C++ 标准、优化(默认为 C++17)等设置。
来自代码教程:
从主菜单中,选择“终端”>“配置默认构建任务”。将出现一个下拉列表,显示 C++ 编译器的各种预定义构建任务。选择C/C++:g++构建活动文件。
这将在文件夹中创建一个tasks.json
文件.vscode
并在编辑器中打开它。
您的新tasks.json
文件应类似于下面的 JSON
我的文件看起来像
任务.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-std=c++17",
"-ggdb",
"-Og",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是我对这个困境的解决方案。我将它与我的项目 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.
Run Code Online (Sandbox Code Playgroud)
Juicy部分在VS代码中;它有一个非常强大的功能,称为任务。任务是它们自己特殊的兔子洞,但坦白地说,您可以将任务添加到工作区中生成的tasks.json 文件中的“tasks”数组中,如果您不熟悉它的外观,这里是任务的语法:
{
"label": "[name of task]",
"type": "[type of task, usually its a shell command]",
"command": "[the actual command to call]"
}
Run Code Online (Sandbox Code Playgroud)
任务可以提供更多功能,但是对于制作构建工具,这将是您所需要的,对我来说,这导致了一个如下所示的文件:
{
// 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"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
为什么我们需要进行最终的构建调用?因为您的 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
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
抱歉,重播时间太长,希望对您有所帮助:)
归档时间: |
|
查看次数: |
20241 次 |
最近记录: |