I want to compile a simple C program with GCC. What do I need to put in the sublime-build file to do so?
-
Already figured out how to do this, but as I couldn't find a proper answer for this elsewhere (not for Mac anyways), I thought I'd post it here. – username Apr 08 '12 at 00:37
-
also see http://stackoverflow.com/questions/16490889/build-and-run-with-arguments-in-sublime-text-2 for how to do this with dynamic arguments (see plugin InputArgs) – Ben Creasy Nov 06 '16 at 05:58
5 Answers
Mac OS X:
{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}"],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}
Windows:
{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe"],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}

- 18,800
- 11
- 41
- 45
-
@Michael Anthony, thanks for the heads-up re: Windows and adding ".exe" – username Jun 22 '12 at 09:52
-
How do you enter values at run time? I mean if we use `scanf( )`, how to enter a value for that instruction during runtime from sublime text? – Greenhorn Jun 08 '15 at 15:25
-
2In case it saves future coders some headache, I just wanted to note that you should place this in a build system config file by going to Tools > Build System > New Build System and NOT in a preferences config file (Preferences > Settings - More > Syntax Specific). – Jerreck Sep 03 '15 at 14:24
-
1Personally, I get `clang: error: no input files` on my Mac where as running `gcc test.c -o test` in my console works. – bafromca Oct 07 '16 at 13:30
-
1@bafromca I get the same issue - if I modify it to look like Scott Fister's answer where the first command is a single string it seems to work: `"cmd": "gcc $file_name -o ${file_base_name}"` – Ben Creasy Oct 24 '16 at 06:40
The accepted answer did not work for me.
What I did is the following:
{
"cmd" : ["make $file_base_name && ./$file_base_name"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path",
}
Setting shell
to true
means it reads the cmd
as one line, so I call make to compile and then run the script. The other option is to have shell
set to false
but you're unable to run multiple cmd
. So the only way I got it to work was have it make the file with CMD + B
and then run it with CMD + Shift + B
:
{
"cmd" : ["make", "$file_base_name"],
"selector" : "source.c",
"shell": false,
"working_dir" : "$file_path",
"variants": [
{
"cmd" : ["./$file_base_name"],
"name": "Run"
}
]
}

- 1,213
- 12
- 24
In windows, compile and run, with file_regex
{
"cmd": ["gcc", "$file_name", "-o", "${file_base_name}.exe", "&&", "${file_base_name}.exe"],
"file_regex": "^([^:]+):([0-9]+):",
"selector": "source.c",
"shell": true,
"working_dir": "$file_path"
}

- 573
- 9
- 19
For Mac
{
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && osascript -e 'tell application \"Terminal\" to activate do script \"clear&&${file_path}/${file_base_name} && read -p \\\"Press Enter to exit.\\\"&&exit\"'"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
For windows
{
"cmd": ["g++", "${file}", "-o","${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:?(.*)$",
"working_dir": "${file_path}",
"encoding":"cp936",
"selector": "source.c",
"variants":
[
{
"name": "Run",
"cmd": ["cmd","/C","start","cmd","/c", "${file_path}/${file_base_name}.exe &pause"]
}
]
}
The configuration files above make you input data in the terminal(Mac) or cmd(windows),the output also was shown in the terminal or cmd。

- 1
- 1
LINUX! COMPILING AND RUNNING IN TERMINAL, LANGUAGE C
Create a new Build System and paste this code:
{
"cmd": ["xterm-256color -e 'zsh -c \"gcc $file_name -o ${file_base_name} && ./${file_base_name} ;echo;echo Presiona ENTER para salir...; read line\"'"],
"selector" : "source.c",
"shell": true
}
echo $SHELL = To know which shell you use (zsh)-------------------------------------------------------------echo $TERM = To know which terminal you use (xterm-256color)-------------------------------------------
:D