How do I compile and run a C program in Sublime Text 2?

I recommend you to read build document of Sublime Text 2.

Here is the answer. In Sublime, click Tools -> Build System -> New Build System…

For Windows user, type the following code and save:

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

For Mac user, type the following code:

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

For Linux User, copy the following code

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

Leave a Comment