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
to true
:
{
"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.
This question is related to
c
gcc
sublimetext3
ubuntu-14.04
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.
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"]
}
try to write a shell script named run.sh in your project foler
#!/bin/bash
./YOUR_EXECUTIVE_FILE
...AND OTHER THING
and make a Build System
to compile and execute it:
{
"shell_cmd": "make all && ./run.sh"
}
don't forget $chmod +x run.sh
do one thing and do it well:)
Are you using sublime text on linux? I got the same problem and it was solved! Here is my c.sublime-build:
{
"shell_cmd" : "gcc $file_name -o $file_base_name && ./$file_base_name",
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}
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.
{
"cmd": ["gcc", "-Wall", "-ansi", "-pedantic-errors", "$file_name", "-o",
"${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],
"selector": "source.c",
"working_dir": "${file_path}",
"shell": true
}
It takes input and show output on a command prompt.
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.
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}"
}
]
}
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"]
}
]
}
The best way would be just to use a Makefile for your project and ST3 will automatically detect build system for your project. For example. If you press shift + ctrl/cmd +B you will see this:
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",
}
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
Source: Stackoverflow.com