我正在基于VSCode设置一个可从USB驱动器运行的便携式环境。我已经在根目录(D :)上安装了MinGW和VScode,并创建了一个包含C ++ env的文件夹。组态。 编辑: 该功能适用于Windows。
因此,我知道为了编译和运行.cc文件,我必须运行Build Task或Task(我只了解基本概念)。我尝试构建应该执行此操作的.json任务,但没有得到任何结果。
我想了解基础知识,因此可以为其他环境创建自己的(简单的).json任务。
这是我到目前为止尝试过的:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Compile&Run",
"command":["D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe"],
"args": [
"-g",
"${file}",
"-o", // I think that this is the problem
"${fileBasenameNoExtension}.out", ",", "./${fileBasenameNoExtension}.out"
],
//I do not fully understand what this next lines mean or do, they came by defaul.
"options": {
"cwd": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
这是我在Linux系统中拥有的task.json,我通过搜索模板来获取它并设法使其正常工作。它正是我需要的。 创建并运行一个.out文件。
{
"version": "2.0.0",
"tasks": [
{
"label": "debug",
"type": "shell",
"command": "",
"args": ["g++","-g", "${relativeFile}", "-o","a.exe"]
},
{
"label": "Compile and run",
"type": "shell",
"command": "",
"args": [
"g++","-g", "${relativeFile}", "-o","${fileBasenameNoExtension}.out", "&&", "clear" , "&&" , "./${fileBasenameNoExtension}.out"
],
"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
}
}
},
]
}
答案 0 :(得分:1)
tasks.json
不是您要用于运行/调试可执行文件的文件。为此,您想使用launch.json
。您可以创建一个依赖于构建任务的启动任务。
此处的文档:https://code.visualstudio.com/Docs/editor/debugging#_launch-configurations
此处(特定于C ++):https://code.visualstudio.com/docs/cpp/launch-json-reference
答案 1 :(得分:0)
由于您非常需要模板,因此我将为您提供反向工程师的职位。 由于这不是一个完整的答案,可能会被否决。但这是我可以帮助您的唯一方法。
{
{
"version": "2.0.0",
"tasks": [
{
"label": "Build & run",
"type": "shell",
"command": "path/to/bin/g++ main.cpp -o main.exe && main",
"problemMatcher":"$gcc",
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": true
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
答案 2 :(得分:0)
const confirmEnding = (str, target) => str.substr(-target.length) === target ?
true :
false;
这很好用
答案 3 :(得分:0)
如果有人正在寻找一种在调试前(按 F5)在 VSCode 中自动构建 cpp 文件的方法,一个简单的解决方案是在 launch.json 文件中添加 "preLaunchTask": "${defaultBuildTask}",
。
这里是我在 linux 中使用 g++ 的文件(你可以删除 smfl 库)
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${workspaceFolder}/src/*.cpp",
"-o",
"${workspaceFolder}/bin/outputfile",
"-lsfml-audio",
"-lsfml-graphics",
"-lsfml-network",
"-lsfml-system",
"-lsfml-window"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
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}/bin/outputfile",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "${defaultBuildTask}",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}