【问题标题】:Visual Studio Code: how to add arguments for g++ compiler?Visual Studio Code:如何为 g++ 编译器添加参数?
【发布时间】:2020-04-14 21:16:38
【问题描述】:

我想使用一些 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++,并在.bash_profile中添加了一个别名alias g++='g++ -std=c++17',这样

g++ test.cpp

会做同样的工作。

我想在 Visual Studio Code - Mac 版本中运行 C++ 代码。在安装了微软的 C/C++ Extension 和 Code Runner Extension 之后,我在 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 您想分享一下如何设置 Makefile 并使用一些热键在 VSCode 中运行的经验吗?

标签: c++ visual-studio-code parameter-passing


【解决方案1】:

假设您使用 C/C++ 扩展,创建一个task.json 文件,它允许您更改编译器路径、包含路径、C++ 标准、优化(默认为 C++17)等设置,等等。

来自代码教程:

从主菜单中,选择Terminal > Configure Default Build Task。将出现一个下拉列表,显示 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++"
        }
    ]
}

【讨论】:

    【解决方案2】:

    这是我对这个困境的解决方案。我将它与我的项目 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.
    

    Juicy 部分在 VS 代码中;它有一个非常强大的功能,称为任务。任务是它们自己的特殊兔子洞,但坦率地说,您将任务添加到工作区中生成的 tasks.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
                }
            ]
        }
    ]
    }
    

    抱歉拖了这么久,希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 2020-03-15
      • 1970-01-01
      • 2020-11-13
      • 2021-06-20
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      相关资源
      最近更新 更多