Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Visual Studio Code tasks to automate C makefiles in multiple folders

I have a project written in C that has two make files, one inside the main directory and one inside of a subdirectory. I would like to use VS Code tasks to automate the make process so I do not have to use the terminal to make the project every time I want to run. Instead, I want to just use ctrl+shift+b and then run my code from the terminal using mpiexec.

I have created the tasks.json file in the .vscode directory which is inside the main folder of my project. The tasks.json currently contains the following code which I based off of this tutorial.

{
    "version": "2.0.0",
    "command": "bash",
    "tasks": [
        {
            "label": "Make Project",
            "type": "shell",
            "command": "cd ${workspaceFolder}",
            "args": ["make"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

I was hoping that this would act the same as typing make into the the terminal while in the location of the current working directory. Instead, this is the output from the terminal

Executing task: 'cd /home/git/project' make <
/bin/bash: cd /home/git/project: no such file or directory
The terminal process terminated with exit code: 127

Terminal will be reused by tasks, press any key to close it.

Note that the make file is located at /home/git/project. I was hoping to then also build a make file at /home/git/project/subfolder.

Why isn't the initial command cd /home/git/project and then make working? Do I need to split this up into several different tasks with a runner? I am new to using VS Code, so any help is appreciated. Thanks.

like image 925
Bhalp Avatar asked Sep 05 '19 15:09

Bhalp


People also ask

How do I use makefiles in VS Code?

If your installation of make is not available in the default path, you can configure it in VS Code at File > Preferences > Settings > Extensions makefile. To compile and link the project, you can add a Makefile to the root of the project folder. It will be detected automatically by the extension.

How do I create a build task in VS Code?

Pressing ⌘ + ⇧ + b or running the command “Tasks: Run Build Task” without having configured a default build task will let you choose among the tasks VS Code is aware of. Similarly, you can configure a default test task. The choice of default task is saved in a file called tasks.


2 Answers

The solution I implemented is as follows:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "msvc build",
            "type": "shell",
            "command": "",
            "args": [
                "make",
                "--directory=${workspaceFolder};",
                "make", 
                "--directory=${workspaceFolder}/subfolder"
            ],
            "group":  {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal":"always"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

Based on both Reinier Torenbeek's suggestion as well as this post on Microsoft's page. The main point to note here is that I am passing no command to the shell and am instead only passing the args.

I can now build all of the makefiles in my project by just pressing ctrl+shift+b inside the editor since the group is set to build.

like image 167
Bhalp Avatar answered Nov 09 '22 07:11

Bhalp


Why isn't the initial command cd /home/git/project and then make working?

The output

Executing task: 'cd /home/git/project' make

shows that the entire expression cd /home/git/project is interpreted as a single command name (including the space, which is a valid character in a filename), with make as its argument. That makes sense when you look at the structure of the json configuration. Since no such command exists, it fails.

There may be several ways to get this to work, the option --directory to make could be a good approach. See 9.7 Summary of Options.

like image 27
Reinier Torenbeek Avatar answered Nov 09 '22 05:11

Reinier Torenbeek