【问题标题】:Errors while building and running C++ code in vscode using gcc. Tasks.json file使用 gcc 在 vscode 中构建和运行 C++ 代码时出错。任务.json 文件
【发布时间】:2020-12-19 06:19:34
【问题描述】:

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

#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 文件命名相同。

这是我面临的错误: image for build error in VSCode terminal

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

Tasks.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
            }
        }
    ]
}

Launch.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
                    }
                ]
            }
        ]
    }

【问题讨论】:

  • 嗯,错误说“没有这样的文件或目录”。那么,您是否检查过您的文件是否存在?并且它存在于正确的路径上?
  • @linuxartisan 是的,文件存在。我能够使用g++ -g hellp.cpp./a.exe 命令成功编译和执行
  • 请提供tasks.json和launch.json文件。
  • @mgonnav 添加了它。
  • 我在 launch.json 中看不到 C++ 的启动配置。但抛开这些不谈,如果你从 tasks.json 中删除选项,你还有问题吗?

标签: c++ gcc visual-studio-code


【解决方案1】:

试试这些,它在我的系统上运行良好

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

【讨论】:

  • PS:我正在使用 MinGW g++ 编译器和安装程序
  • 你不需要launch.json来编译和运行c++代码launch.json是你使用VSCODE DEBUGGER时使用的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
  • 2023-03-23
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多