user2980746
user2980746

Reputation: 381

vscode makefile: is there a way to run the makefile when pressing F5?

I installed the makefile extension for C/C++ in VSCode and in the makefile side panel I can press the makefile build button and it works.

I was wondering if there is a way to run the makefile when pressing F5 though? I'm guessing I need to add a command to my launch.json but I'm not sure what to do. I see the launch configuration has a setting called "preLaunchTask" which maybe I could create a task to start the makefile?

Here is my makefile configuration


    "makefile.configurations": [
        {
            "name":"Make main"
        }
    ],
    "makefile.buildBeforeLaunch": true,
    "makefile.clearOutputBeforeBuild": true,
    "makefile.launchConfigurations": [
        {
            "cwd": "c:\\Users\\sferr\\OneDrive\\Desktop\\dev",
            "binaryPath": "c:\\Users\\sferr\\OneDrive\\Desktop\\dev\\main",
            "binaryArgs": []
        }
    ],

Upvotes: 0

Views: 127

Answers (1)

user2980746
user2980746

Reputation: 381

I figured it out from this post, Using Visual Studio Code tasks to automate C makefiles in multiple folders.

This took me like two days to figure out how to do this so i'll post the steps here.

  1. Download vscode and install the makefile extension as seen in this post, https://earthly.dev/blog/vscode-make/.

  2. Create a makefile in the root directory of your folder. Mine is very simple and looks like this.

all: build run clean

build: main.o loop.o utility.o
    gcc main.o loop.o utility.o -o main

main.o: main.c src/manager/manager.h
    gcc -c main.c

loop.o: src/loop/loop.c src/loop/loop.h
    gcc -c src/loop/loop.c 

utility.o: src/utility/utility.c src/utility/utility.h
    gcc -c src/utility/utility.c

run:
    ./main.exe

clean:
    rm *.o

2.5 Click on the makefile button in your activity bar and set the configuration, build target, launch target, etc for the makefile.

  1. Your project should have a ".vscode" folder with settings.json, tasks.json, and launch.json files.

  2. In the settings.json file put this.

    "makefile.configurations": [
        {
            "name":"Make main"
        }
    ],
    "makefile.buildBeforeLaunch": true,
    "makefile.clearOutputBeforeBuild": true,
    "makefile.launchConfigurations": [
        {
            "cwd": "c:\\Users\\sferr\\OneDrive\\Desktop\\dev",
            "binaryPath": "c:\\Users\\sferr\\OneDrive\\Desktop\\dev\\main",
            "binaryArgs": []
        }
    ],
  1. In the tasks.json file put this.
        {
            "label": "build",
            "type": "shell",
            "command": "",
            "args": [
                "make",
                "--directory=${workspaceFolder};"
            ],
            "group":  {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal":"always"
            },
            "problemMatcher": "$msCompile"
        }
  1. And in the launch.json file put this.
    "configurations": [
        {
            "name": "Run",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\main.exe",
            "cwd": "c:\\Users\\sferr\\OneDrive\\Desktop\\dev",
            "preLaunchTask": "build"
        }
    ]

It should run your makefile now when you press f5.

Upvotes: 0

Related Questions