cvisual-studio-codecompilationvscode-code-runner

How do I customize C compile and run in Visual Studio Code?


I have always compiled and run my C project manually in this way:

gcc -o main file1.c file2.c main.c
./main

If I open my project on VS Code and try to compile and run it by pressing the "play" button, VS Code first writes on terminal:

gcc main.c -o main

which will make obviously fail the compile, because the other needed files (file1.c and file2.c) are missing in compilation.

Where (on VS Code) can I customize the compile and run process made by the "play" button from the Code Runner extension?


Solution

  • For customizing the Code Runner play button, modify your code-runner.executorMap setting.

    But I'd instead recommend a build task, since Code runner is more suited to building and running individual files, and by editing the executor map for a specific set of files would kind of be subverting its purposes, whereas this kind of thing is what build tasks are designed for.

    A build task for you might look something like this:

    {
       "type": "shell",
       "label": "C/C++: gcc build main",
       "command": "gcc",
       "args": ["-o", "main", "file1.c", "file2.c", "main.c"],
       "options": {
          "cwd": "${workspaceFolder}"
       },
       "problemMatcher": ["$gcc"],
       "group": {
          "kind": "build",
          "isDefault": true
       },
    }
    

    See also https://code.visualstudio.com/docs/languages/cpp#_tutorials.