Visual Studio Code C++ with Makefile Cheat
Tags: vscode, codenewbie, cpp
Published: 2020-07-21 23:44:11

This will be real quick, but I found out something, mostly on my own and I want to share it if anyone else needs it.

I've been studying and using Visual Studio Code on Linux for about a month now. Some technologies (like .NET core, C#) seem to work easily.  Need a build task?  No problem, voila! You have a build task.

Some technologies (like C), you're sort of on your own.  Need a build task?  Googling...fumbling...got it.  Not too many problems.

But, one, I've had a great deal of difficulty with.  Some of it my fault.  Some of it...I just want to do it differently.

I'm talking about building C++ files on vscode.  

Vscode (for short), with the proper extension, does have a built in task for the "build my files" command.  However, I was having problems making it work.  In addition, I had decided I wanted to use Makefiles instead as I'm familiar with those.  I was using a task template for building C files with Makefiles and just wanted the two (C and C++) to be consistent.

However, it wasn't that easy.  The "My fault" part got worked out with a question posted on Twitter.  I was trying to build while my active file was the tasks.json file.  Bang head on desk moment.

But, when I wanted to find the same generic task template I used for building C files, vscode just wouldn't give me the option.  I discovered today that if I simply select 'Configure a Task' instead of 'Configure a Build Task', it will give me the generic template option (Other).

It's taken a little bit of work and googling, but I now have a template tasks.json file that I can paste over the generic template that will run the Makefile in the same folder as my *.cpp files.  I've even added a 'make clean' task for when needed.

Vscode uses the name you give your task ( the "label" line) along with a build identifier ("group":"build") to place each task in the appropriate menu.  So, if you use the following template in place of a generic tasks.json file (in your folder where you're working on *.cpp files), it will show up in a request to build and as an item when you request to run a task. Remember that there are two tasks in the file, separated by commas. The first is the build with make.  The second is the make clean task, and it doesn't get the "group":"build" portion.

Feel free to kibitz (nicely) my work.  I'm still pretty new to this and there is probably an easier way.  However, for me, this way works to build my C++ projects in Visual Studio Code.

Completely paste this over the generic tasks.json file, or you can insert it as a file named the same in your .vscode folder.

```
file: tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build dbc from Makefile",
            "type": "shell",
            "command": "make",
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        },
        {
            "label": "Run make clean on folder",
            "type": "shell",
            "command": "make clean",
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        }
    ]
}
```