c++ - 使用 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
}
]
}
]
}
解决方案
试试这些它在我的系统上工作正常
{
"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
}
]
}
]
}
推荐阅读
- postgresql - 将 PostgreSQL 数据目录更改为 PostgresSQL 在另一台机器上创建的目录
- python - geopy 不会为加拿大邮政编码返回正确的坐标
- excel - 有没有办法在用户窗体上嵌套相关的组合框?
- c - 在 Netfilter 中分配更多有效负载空间的正确方法?
- c# - 在 .ics 文件的 DateTime 对象中指定时区 - 时间相差一个小时(C#,Sitecore 9)
- typescript - 是否有禁止前缀类型断言语法的 TSLint 规则?
- filemaker - FileMaker 错误:无法在此磁盘上创建 PDF
- java - 错误无法打开属性文件:来自 Ant 脚本的 AppManage.tra
- python - 这段代码有什么问题?它没有显示任何错误,但也没有显示任何输出
- report - 将DataBand放在某个空间