Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a .JSON task for C++ compiling and running? (VScode)

I'm setting up a portable enviroment based in VSCode that can run from a USBdrive. I've installed MinGW and VScode, at the root directory (D:) and created a folder that will contain the C++ env. configuration. Edit: This is intended to work on Windows.

enter image description here

So, I know that in order to compile and run a .cc file I have to run a Build Task or Task (I just understand the basic concept). I've tried to build the .json task that should do that but I'm not gettig any result.

I would like to understand the basics so I can create my own (and simple) .json tasks for other enviroments.

This is what I tried so far:

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

}

This is the task.json I have in my linux system, I got it by searching templatesand managed to make it work. It does just what I need. Creates and run a .out file.

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

    ]
}
like image 796
Pauete Galopa Avatar asked Sep 12 '25 07:09

Pauete Galopa


1 Answers

If someone is looking for a way to auto build the cpp files in VSCode before debug (on pressing F5), a simple solution is to add "preLaunchTask": "${defaultBuildTask}", in the launch.json file.

Here are my files in linux with g++ for example (you can remove the smfl libs)

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
                }
            ]
        }
    ]
}
like image 72
Aniket Avatar answered Sep 14 '25 22:09

Aniket