How to compile and run C in sublime text 3?

Have you tried just writing out the whole command in a single string?

{
"cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path"
}

I believe (semi-speculation here), that ST3 takes the first argument as the "program" and passes the other strings in as "arguments". https://docs.python.org/2/library/subprocess.html#subprocess.Popen


For a sublime build system implementing the Run menu command :

  • Go to Tools->Build System->New Build System...

Or

  • Create a file ~/.config/sublime-text-3/Packages/User/GCC.sublime-build

And insert this:

{
"shell_cmd" : "gcc $file_name -o ${file_base_name}",
"working_dir" : "$file_path",
"variants":
  [
    {
      "name": "Run",
      "shell_cmd": "gcc $file_name -o ${file_base_name} && ${file_path}/${file_base_name}"
    }
  ]
}

*This example uses the GCC compiler. Feel free to replace gcc with the compiler of your choice.


After a rigorous code-hunting session over the internet, I finally came up with a solution which lets you compile + run your C code "together at once", in C99, in a dedicated terminal window. I know, a few people dont like C99. I dont like a few people either.

In most of the cases Sublime compiles and runs the code, but in C90 or a lesser version. So if you specifically want it to be C99, this is the way to go.

NOTE: Btw, I did this on a Windows machine, cannot guarantee for others! It probably won't work there.

1. Create a new build system in Sublime: Tools > Build System > New Build System...

New Build System

2. A new file called untitled.sublime-build would be created.

Most probably, Sublime will open it for you.

If not, go to Preferences > Browse Packages > User

Open new sublime-build

If the file untitled.sublime-build is there, then open it, if it isn't there, then create it manually and open it.

3. Copy and paste the given below code in the above mentioned untitled.sublime-build file and save it.

{
    "windows":
    {
        "cmd": ["gcc","-std=c99" ,"$file_name","-o", "${file_base_name}.exe", "-lm", "-Wall", "&","start", "${file_base_name}.exe"]
    },
    "selector" : "source.c",
    "shell": true,
    "working_dir" : "$file_path",
}

Close the file. You are almost done!

4. Finally rename your file from untitled.sublime-build to myC.sublime-build, or you might as well show your creativity here. Just keep the file extension same.

5. Finally set the current Build System to the filename which you wrote in the previous step. In this case, it is myC

Update current build system

Voila ! Compile + Run your C code using C99 by Tools > Build , or by simply pressing Ctrl + B


We can compile the code of C in Sublime Text and can print some value or strings but it does not accept input from the user. (Till I know... I am sure about compiling but not about output from given input.) If you are using Windows you have to set the environment variables for Sublime Text and GCC compiler.