Skip to content

Instantly share code, notes, and snippets.

@akanshgulati
Last active April 23, 2024 17:48
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save akanshgulati/56b4d469523ec0acd9f6f59918a9e454 to your computer and use it in GitHub Desktop.
Save akanshgulati/56b4d469523ec0acd9f6f59918a9e454 to your computer and use it in GitHub Desktop.
Visual Studio Code task to compile and run C programs

Introduction

The below code is the configuration for the Microsoft Visual Code tasks which will enable you to compile and run C program

Steps

  1. Press Cmd + Shift + P
  2. Type Configure task ( A task.json file will be created for that project )
  3. Copy below configuration code and save it.

Usage

Simple press Cmd + Shift + B to compile and run.

Note: Make sure you select the tab having C program as below tasks run on active tab in VS Code.

Task Config

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile and run C",
            "type": "shell",
            "command": "gcc ${file} -o ${fileBasenameNoExtension}  && ./${fileBasenameNoExtension} ",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
@ukulsh
Copy link

ukulsh commented Jun 26, 2018

its giving an error && in not a valid statement separator in this verson

@lanzorg
Copy link

lanzorg commented Aug 23, 2018

@ukulsh
You have to use ; instead of && on Windows.

@deekshithanand
Copy link

Isn't it possible to have 2 different task files so that you can have 2 different keybindings assigned to
them and run build task and run task separately?

@aman-shaw
Copy link

thank you

@Yazidn
Copy link

Yazidn commented Jul 25, 2020

Thank you so much, greatly appreciated!

@zawie
Copy link

zawie commented Feb 10, 2021

Huge time saver. Thank you!

@Neil-Lunavat
Copy link

On Windows 10, there is a problem where if the directory name has a space in its name, it throws an error and does not compile.

Executing task: gcc "C:\Users\Admin\Desktop\c-projects\Game Development\Main.c" -o Main ; ./Main <

gcc.exe: error: C:\Users\Admin.NEIL\Desktop\c-projects\Game: No such file or directory
gcc.exe: error: Development\Main.c: No such file or directory
gcc.exe: fatal error: no input files
compilation terminated.

There is a space between Game and Development. How do I fix this?

@Neil-Lunavat
Copy link

Neil-Lunavat commented Mar 3, 2021

Never mind, I got it. Here's the fix if anyone else wants:
Change the "command" line to this:

"command": "gcc '${file}' -o ${fileBasenameNoExtension} ; ./${fileBasenameNoExtension} ",

Just gotta put single quotes around all file path variables. (There's only one in this case.) Also, change the ; semicolon as needed. My OS is Windows 10.

@Neil-Lunavat
Copy link

Hey, what if I want to compile all .c files?

@akanshgulati
Copy link
Author

@Neil-Lunavat You need to use c compiler instead of gcc mentioned in command.

@bphermansson
Copy link

Thanks!

@joshi248
Copy link

joshi248 commented Jun 7, 2023

How do I run it in an external terminal like cmd instead of integrated one

@MikeBonWebDev
Copy link

Hey guys, thanks for support. I was looking for a solution and I find it! Thank you so much! Work perfectly! @akanshgulati @Neil-Lunavat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment