Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Visual Studio代码:如何为g++;编译程序?_C++_Visual Studio Code_Parameter Passing - Fatal编程技术网

C++ Visual Studio代码:如何为g++;编译程序?

C++ Visual Studio代码:如何为g++;编译程序?,c++,visual-studio-code,parameter-passing,C++,Visual Studio Code,Parameter Passing,我想使用Mac的clang目前不支持的一些C++17功能,所以我使用 brew install gcc --HEAD 安装g++10.0.1版本。通过直接调用,代码在终端中运行良好 g++-HEAD -std=c++17 test.cpp 我还在bash中创建了一个链接ln-sg++-headg++,并在.bash\u profile中添加了一个别名alias g++='g++-std=c++17',以便 g++ test.cpp 我也会做同样的工作 我想在VisualStudio代码-M

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

brew install gcc --HEAD
安装g++10.0.1版本。通过直接调用,代码在终端中运行良好

g++-HEAD -std=c++17 test.cpp
我还在bash中创建了一个链接
ln-sg++-headg++
,并在
.bash\u profile
中添加了一个别名
alias g++='g++-std=c++17'
,以便

g++ test.cpp
我也会做同样的工作

<>我想在VisualStudio代码-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++编译器不是我用alias手动设置的编译器。更有趣的是,如果我直接在VSCode终端中运行,我以前的代码

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代码;它有一个非常强大的功能,称为任务。任务本身就是一个特殊的兔子洞,但坦率地说,您将任务添加到工作区中生成的Tasks.json文件中的“Tasks”数组中,如果您不熟悉它的外观,下面是任务的语法:

    {
        "label": "[name of task]",
        "type": "[type of task, usually its a shell command]",
        "command": "[the actual command to call]"
    }
tasks可以提供更多的功能,但对于制作构建工具来说,这将是您所需要的全部,对我来说,这将生成一个如下所示的文件:

{
// 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"
        ]
    }
]
}
{
// 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
            }
        ]
    }
]
}
为什么我们需要最终的构建调用?因为您的launch.json对象可以接受“preLaunchTask”,它将在调试之前自动调用。您可以插入最后的构建调用,它将编译、调试器,然后运行您的应用程序。它甚至将GDB断点和内存跟踪集成到工作区中。My launch.json如下所示:

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

<> P> >抱歉,对于长时间重放,我希望它有帮助:

< P>假设你使用C/C++扩展,创建一个<代码>任务.JSON<代码>文件,它允许你改变设置,比如路径到编译器,包括路径,C++标准,优化(默认是C++ 17),等等。 从代码教程:

从主菜单中,选择终端>配置默认生成任务。下拉出现了C++编译器的各种预定义的构建任务。选择C/C++:g++生成活动文件

这将在
.vscode
文件夹中创建一个
tasks.json
文件,并在编辑器中打开它

新的
tasks.json
文件应该与下面的json类似

我的文件看起来像

tasks.json


我把我的设置在一个生成文件中。对您来说,这可能是一种更具伸缩性的方法。然后,您可以在任务中简单地运行“make”。它会更干净,也更容易管理:)@Pnelego你想分享一下你的经验吗?关于如何设置Makefile并使用一些热键在VSCode中运行?