Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile and run C in sublime text 3?

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.

like image 692
CMPS Avatar asked Jun 14 '14 23:06

CMPS


People also ask

How do I compile and run a .c file?

Step 1: Open turbo C IDE(Integrated Development Environment), click on File and then click on New. Step 2: Write the C program code. Step 3: Click on Compile or press Alt + F9 to compile the code. Step 4: Click on Run or press Ctrl + F9 to run the code.

How do I run C and C++ in Sublime Text?

JSON for Package New Build Package in Sublime Text We have to go Tools > Build System > New Build System and then add the following code and save the file: Finally, We can now build our code by pressing Command + B and see the outputs for the inputs of in. txt file into out. txt file.


Video Answer


1 Answers

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

like image 53
erbridge Avatar answered Sep 21 '22 03:09

erbridge