Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

gcc - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...