Home > Back-end >  When writing a C , if the source file is being saved, compile it
When writing a C , if the source file is being saved, compile it

Time:12-05

I have this question : when I save a C source file in VsCode, I always need to run a task through this command, then : this one, translated to English would be : "Compile this C active file using g compiler". I would like to know if there was a way to make sure that if the file is saved it will be also compiled. I tried to search everything possible but I really couldn't file something useful, plus I am not very familiar to .json language.

Infos :

Code Editor : Visual Studio Code

Task Language : .json

Compiler : g Version 2.0.0

Terminal Used For Compiling : Windows PowerShell

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C  : g  .exe compila il file attivo",
      "command": "C:\\msys64\\mingw64\\bin\\g  .exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": ["$gcc"],
      "group": "build",
      "detail": "compilatore: C:\\msys64\\mingw64\\bin\\g  .exe"
    }
  ]
}

The normal procedure is to save the file, use the commands I put above, and then run the code from the terminal.

CodePudding user response:

EDIT: Sorry, my old approach didn't work and I noticed when re-reading the task.

In order to automatically run the build task when a file is saved, you should use the "file_watcher" (instead of my previous implemention) setting in the .vscode/settings.json file:

{
  "files.autoSave": "afterDelay",
  "files.watcherExclude": {
    "**/.git/objects/**": true,
    "**/.git/subtree-cache/**": true,
    "**/node_modules/**": true
  },
  "file_watcher": {
    "c:\msys64\mingw64\bin\g  .exe": ["${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"]
  }
}

You will need to update the paths in the file_watcher setting to match your local configuration. You may also need to modify the build command and its arguments to match your project's requirements.

  • Related