3

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

    ]
}
1
  • 1
    change .out to .exe, windows won't automatically execute a .out file. You also don't need the ./, thats for Unix based systems. cwd is the current working directory, should probably be set to your workspace directory rather than the compiler location. Commented Sep 18, 2019 at 14:47

4 Answers 4

3

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
                }
            ]
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

2

Since you pretty much want a template, I will post mine for you to reverse engineer. This will probably get down voted since it's not a complete answer, but it's the only way I can help you.

{
    {
    "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 Comments

Mhh, mine works fine, since it compiles de .cc file, the problem lies when trying to run it. Note: the && conector(?) it doesn't work for me, don't know why tho. Thanks anyway!
Maybe you're using powershell instead of cmd as the shell?
1

tasks.json is not the file you want to use for running/debugging an executable. For that, you want to use launch.json. You can create a launch task that takes a dependency on a build task.

Documentation here: https://code.visualstudio.com/Docs/editor/debugging#_launch-configurations
and here (C++ specific): https://code.visualstudio.com/docs/cpp/launch-json-reference

Comments

0
{
"version": "2.0.0",
    "tasks": [
      
        {
            "label": "Compile and run",
            "type": "shell",
            "command": "g++",
            "args": [
            
                "-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
                }
            }
        },
    ]
}

This is working very fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.