首页 > 解决方案 > 使用 gcc 在 vscode 中构建和运行 C++ 代码时出错。任务.json 文件

问题描述

这是我试图构建和执行的代码。

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
     cout << "Hello World";
     return 0;
}

我能够使用命令编译和运行它

directory path:// g++ -g hello.cpp

./a.exe

我现在制作了一个tasks.json文件,这样我就不必每次都运行上述两个命令,并且该.exe文件的名称也与该.cpp文件相同。

这是我面临的错误: VSCode 终端中构建错误的图像

如果有人知道解决此问题的方法,请帮助我。tasks.json如果您需要文件内容,请告诉我。我会把它们放在这里。

任务.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

启动.json

{
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C/C++ (gdb) Launch",
                "type": "cppdbg",
                "request": "launch",
                "program": "C:/Users/H.P/.vscode/CC++_gcc_Compiler/bin/gdb.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceRoot}",
                "environment": [],
                "externalConsole": true,
                "MIMode": "gdb",
                "miDebuggerPath": "C:/Users/H.P/.vscode/CC++_gcc_Compiler/bin/gdb.exe",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }

标签: c++gccvisual-studio-code

解决方案


试试这些它在我的系统上工作正常

{
"version": "2.0.0",
    "tasks": [
      
        {
            "label": "Compile and run",
            "type": "shell",
            "command": "cls",
            "args": [
                ";",
                "g++",
                "-g",
                "${file}",
                "-o",
                "ans.exe",
                ";",
                // "cls",
                // ";",
                "./ans.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.):(\\d+):(\\d+):\\s+(warning|error):\\s+(.)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
    ]
}

和 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}/ans.exe",
            "args": [],
            "stopAtEntry": false,
            "externalConsole":true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

推荐阅读