How to compile and run C in sublime text 3?
Solution 1
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
Solution 2
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.
Solution 3
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
Solution 4
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.
Solution 5
Instruction is base on the "icemelon" post. Link to the post:
how-do-i-compile-and-run-a-c-program-in-sublime-text-2
Use the link below to find out how to setup enviroment variable on your OS:
The instruction below was tested on the Windows 8.1 system and Sublime Text 3 - build 3065.
1) Install MinGW. 2) Add path to the "MinGW\bin" in the "PATH environment variable".
"System Properties -> Advanced -> Environment" variables and there update "PATH' variable.
3) Then check your PATH environment variable by the command below in the "Command Prompt":
echo %path%
4) Add new Build System to the Sublime Text.
My version of the code below ("C.sublime-build").
link to the code:
// Put this file here:
// "C:\Users\[User Name]\AppData\Roaming\Sublime Text 3\Packages\User"
// Use "Ctrl+B" to Build and "Crtl+Shift+B" to Run the project.
// OR use "Tools -> Build System -> New Build System..." and put the code there.
{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe"],
// Doesn't work, sublime text 3, Windows 8.1
// "cmd" : ["gcc $file_name -o ${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path",
// You could add path to your gcc compiler this and don't add path to your "PATH environment variable"
// "path" : "C:\\MinGW\\bin"
"variants" : [
{ "name": "Run",
"cmd" : ["${file_base_name}.exe"]
}
]
}
CMPS
SOreadytohelp Algorithm improvement and implementation Java C PHP MySQL HTML CSS JavaScript JQuery Web Security Java Swing
Updated on January 18, 2021Comments
-
CMPS over 3 years
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 below code), but I was wondering if it's possible to have the program execution output to appear on sublime text console as well.
Here's what I currently have to compile C program with sublime text 3
c_compile.sublime-build
{ "cmd" : ["gcc", "$file_name", "-o", "${file_base_name}"], "selector" : "source.c", "shell":false, "working_dir" : "$file_path" }
I've tried adding
&& ./${file_base_name}
like this:{ "cmd" : ["gcc", "$file_name", "-o", "${file_base_name}","&&","./${file_base_name}"], "selector" : "source.c", "shell":false, "working_dir" : "$file_path" }
But it's giving me this error:
gcc: error: &&: No such file or directory [Finished in 0.0s with exit code 1] [cmd: ['gcc', 'Ex1-6.c', '-o', 'Ex1-6', '&&', './Ex1-6']] [dir: /home/admin/Desktop/C/book/chap1] [path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]
Here's my simple C program I'm working with:
Ex1-6.c
#include <stdio.h> main(){ printf("Hello world"); }
I searched online for a solution but suggested answers either allows to compile only (This parts is already working for me), or does not work. Any idea how to fix this code in order to compile and run in sublime text 3 (If possible). Thank you
Edit #1 as suggested by user2357112:
After changing
shell
totrue
:{ "cmd" : ["gcc", "$file_name", "-o", "${file_base_name}","&&","./${file_base_name}"], "selector" : "source.c", "shell":true, "working_dir" : "$file_path" }
That's what I get:
gcc: fatal error: no input files compilation terminated. [Finished in 0.0s with exit code 4] [cmd: ['gcc', 'Ex1-6.c', '-o', 'Ex1-6', '&&', './Ex1-6']] [dir: /home/admin/Desktop/C/book/chap1] [path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]
Edit #2 as suggested by Eugene K:
I tried changing cmd to run the program only:
{ "cmd" : ["./${file_base_name}"], "selector" : "source.c", "shell":false, "working_dir" : "$file_path" }
It runs successfully and prints the output on the console with some code:
Hello world [Finished in 0.0s with exit code 12] [cmd: ['./Ex1-6']] [dir: /home/amir/Desktop/C/book/chap1] [path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]
So far the cmd either compiles or runs but does not do both together, hope something can be done to make it compile and run with a single command.
-
CMPS about 10 yearsI forgot to mention I'm using Ubuntu 14.04
-
rock321987 about 10 yearsstill..i don't think it works..i also wonder to run the program in sublime text..+1 for the question..maybe someone gives the answer
-
CMPS about 10 yearsThank you, currently I am able to compile using the first block of code, but my problem is in running the program. Hopefully someone will have a solution (if possible) :)
-
Eugene K about 10 yearsCurious of this solution, would be inline with how I thought it must function.
-
CMPS about 10 yearsThanks this actually works with having shell = true; The only issue which I think is not solvable is getting input from the user.
-
erbridge about 10 yearsAFAIK, ST3 doesn't support user input in its build systems.
-
Jerreck almost 9 yearsIn 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).
-
Jonath P about 7 yearsWhat edits would be needed to show all warnings and errors that might occur during the compilation?