Visual Studio Code:如何为 g++ 编译器添加参数? [英] Visual Studio Code: how to add arguments for g++ compiler?

查看:227
本文介绍了Visual Studio Code:如何为 g++ 编译器添加参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一些 Mac 的 clang 目前不支持的 C++17 功能,所以我使用

I want to use some C++17 features that Mac's clang doesn't currently support, so I use

brew install gcc --HEAD

安装 g++ 10.0.1 版本.直接调用代码在终端运行良好

to install the g++ 10.0.1 version. Codes run well in terminal by directly calling

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

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

I also created a link ln -s g++-HEAD g++ in bash, and added an alias alias g++='g++ -std=c++17' in .bash_profile, so that

g++ test.cpp

会做同样的工作.

我想在 Visual Studio Code - Mac 版中运行 C++ 代码.在安装了 Microsoft 的 C/C++ 扩展和 Code Runner 扩展后,我在 VSCode 中设置了 settings.json 文件以包含编译器参数:

I want to run C++ code in Visual Studio Code - Mac version. After installing its C/C++ Extension by Microsoft, and Code Runner Extension, I set up the settings.json file in VSCode to include compiler argument:

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

然后我尝试运行相同的代码.但是,我收到警告:

Then I tried to run the same code. However, I got warning:

[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中运行,我之前的代码

Clearly, it means the g++ compiler ran in VSCode is not the one I manually set with alias. More interestingly, if I run directly in VSCode TERMINAL, my previous code

g++ test.cpp -o test

有效.

我对 VSCode 中的设置感到困惑:为什么运行程序不使用与 VSCode 自己的终端中使用的相同的 g++ 编译器参数?另外,我应该如何修改 settings.json 文件或 VSCode 中的其他一些文件,以便我可以正确添加 -std=c++17 参数?

I'm confused by the setup in VSCode: why doesn't the runner use the same g++ compiler argument as used in VSCode's own terminal? Also, how should I modify the settings.json file or some other files in VSCode so that I can correctly add the -std=c++17 argument?

推荐答案

这里是我对这个困境的解决方案.我将它与我的项目 Makefile 相关联.

Here is my solution to this predicament. I link it with my projects Makefile.

首先,您需要构建一个 Makefile,如果您使用 GCC 使用 Makefile 本身是......至少可以说是有争议的.但我认为它符合这个例子的目的.您可能知道,在当前目录中运行make"而不是 g++ 将解析 Makefile 并运行相应的命令.但就您而言,您的 Makefile 可能类似于:

First, you'll need to build a Makefile, if your using GCC using a Makefile on its own is... controversial to say the least. But I think it serves the purposes of this example. As you may know, running "make" instead of g++ in the current directory will parse the Makefile and run the corresponding commands. But in your case, your Makefile may look similar to:

#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.

Juicy 部分在 VS 代码中;它有一个非常强大的功能,称为任务.任务是它们自己的特殊兔子洞,但坦率地说,您可以在工作区中生成的 tasks.json 文件中的tasks"数组中添加一个任务,如果您不熟悉它的外观,这里是任务的语法:

The Juicy part is in VS code; It has a very power feature known as tasks. Tasks are their very own special rabbit hole, but to put it bluntly, you add a task to the "tasks" array in a generated tasks.json file in your workspace, if you're not familar with how that might look, here is the syntax for a task:

    {
        "label": "[name of task]",
        "type": "[type of task, usually its a shell command]",
        "command": "[the actual command to call]"
    }

任务可以提供更多功能,但是对于制作构建工具,这将是您所需要的全部功能,对我来说,这导致了一个如下所示的文件:

There are many more features that tasks can offer, but for making a build tool this will be all you need, for me, this resulted in a file that looked like this:

{
// 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 看起来像这样:

Why do we need to have a final build call? because your launch.json object can take a "preLaunchTask" that will automatically call prior to debug. You can slap in that final build call, and it will compile, debugger, and then run your app. it will even integrate GDB breakpoints and memory tracking into the workspace. My launch.json looks like this:

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

抱歉,重播时间过长,希望对您有所帮助:)

Sorry for the long replay, i hope it helps :)

这篇关于Visual Studio Code:如何为 g++ 编译器添加参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆