1

I usually do competitive programming in Geany IDE and have the following custom compile command to compile C++ programs -

g++ -std=c++17 -Wshadow -Wall -o "%e" "%f" -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG

The compile command is binded by f9 key. I just have to press f9, which saves and compiles the file and then I switch to bash terminal (f2 key shortcut) to execute the binary file. The terminal also follows the path of the current file opened in editor.

I want the same settings in VS Code. So far, I have managed to bring the editor and terminal side by side and to easily toggle the focus between them via f1 and f2.

But I am unable to set the custom compile command, bind it with f9 key and to configure the terminal so that it follows the path of file in editor currently in focus.
Please give me a complete solution. It would be much better to edit the json setting files directly.
Here is a snapshot of the settings in my Geany IDE :- This is how my Geany looks and the Setting boxes

sushruta19
  • 327
  • 3
  • 12
  • compilation is done by Tasks – rioV8 Apr 04 '22 at 15:08
  • Look at the terminal `sendSequence` command in a keybinding for the first part. – Mark Apr 04 '22 at 19:10
  • This [answer](https://stackoverflow.com/questions/65015057/how-to-compile-c-language-with-different-options-with-vscode/65015872#65015872) from a while ago provides some details about being able to *build* on linux/windows the same project. – Tryer Apr 05 '22 at 10:53

1 Answers1

3

You can set a custom task in VS Code. Edit the tasks.json file in the .vscode folder of your workspace like:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "My build task",
            "type": "shell",
            // Assign output file name with VSCode inner variables like ${fileBasename}
            "command": "g++ -std=c++17 -Wshadow -Wall -o ${fileBasename} -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG",
            "options": {
            },
            "problemMatcher": ["$gcc"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

You can write the compile command directly into "command" attribute, or write it with more commands into a script then write the simple executing script command in the attribute instead.

And the "isDefault": true attribute make this default task which could be invoked simply binding with ctrl+shift+B.

rustyhu
  • 1,912
  • 19
  • 28
  • Thank you so much. This not only helped me but also taught me many new stuff. I added the vscode inner variable ${fileBasenameNoExtension} as output file in the build command. – sushruta19 Apr 05 '22 at 16:14
  • Is there any tasks.json file at the user level, not just workspace. – sushruta19 Apr 05 '22 at 16:40
  • 1
    AFAIK no, refering to the doc: "Task support is only available when working on a workspace folder...". – rustyhu Apr 05 '22 at 16:47