Visual Studio Code:如何为g++编译器添加参数?

5

我希望使用一些C++17特性,但Mac的clang目前不支持,因此我使用

brew install gcc --HEAD

安装 g++ 10.0.1 版本。直接调用终端中的代码可以正常运行。

g++-HEAD -std=c++17 test.cpp

我还在bash中创建了一个链接ln -s g++-HEAD g++,并在.bash_profile中添加了一个别名alias g++='g++ -std=c++17',以便

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++编译器参数?另外,我应该如何修改settings.json文件或其他VSCode文件,以便我可以正确添加-std=c++17参数?


我已经在Makefile中设置好了我的项目。这可能是对你来说更可扩展的方法。然后你可以在任务中简单地运行“make”。这将会更加清晰和易于管理 :) - Pnelego
@Pnelego,您是否愿意分享一下如何在VSCode中设置Makefile并使用一些快捷键运行的经验? - Heifetz_Fan
2个回答

8
假设您使用C/C++扩展程序,请创建一个task.json文件,它将允许您更改设置,如编译器路径、包含路径、C++标准、优化(默认为C++17)等。
从代码教程中:
从主菜单选择终端 -> 配置默认构建任务。下拉菜单显示了各种预定义的C++编译器构建任务。选择C/C++:g++ build active file
这将在.vscode文件夹中创建一个tasks.json文件并在编辑器中打开它。
您的新的tasks.json文件应该类似于下面的JSON。我的文件看起来像:

tasks.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++"
        }
    ]
}

1
这里是我对这个困境的解决方案。我将它与我的项目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 Code;它有一个非常强大的功能,称为任务。任务是它们自己特殊的兔子洞,但简单来说,您可以将任务添加到工作区中生成的tasks.json文件的“tasks”数组中。如果您不熟悉它的外观,这里是任务的语法:

    {
        "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
            }
        ]
    }
]
}

对不起回复这么晚,希望能有所帮助 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接