I would like to compile and run C program in sublime text 3 on ubuntu 14.04. Currently the program is being compiled with gcc using sublime text 3 executing a command (see b
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...
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
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
Voila !
Compile + Run your C code using C99 by Tools > Build , or by simply pressing Ctrl + B
Or
~/.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.
In Sublime Text 3....Try changing the above code to this, note the addition of "start".....
"variants" : [
{ "name": "Run",
"cmd" : ["start", "${file_base_name}.exe"]
}
The latest build of the sublime even allows the direct command instead of double quotes. Try the below code for the build system
{
"cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path",
}
The code that worked for me on a Windows 10 machine using Sublime Text 3
{
"cmd" : "gcc $file_name -o ${file_base_name}",
"selector" : "source.c",
"shell" : true,
"working_dir" : "$file_path",
"variants":
[
{
"name": "Run",
"cmd": "${file_base_name}"
}
]
}
If you code C or C++ language. I think we are lucky because we could use a file to input. It is so convenient and clear. I often do that. This is argument to implement it :
{
freopen("inputfile", "r", stdin);
}
Notice that inputfile must locate at same directory with source code file, r is stand for read.