如何在崇高的文本3编译和运行C? [英] How to compile and run C in sublime text 3?

查看:265
本文介绍了如何在崇高的文本3编译和运行C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ubuntu 14.04上以崇高的文本3编译并运行C程序。目前该程序正在使用gcc编译使用崇高的文本3执行一个命令(见下面的代码),但我想知道是否有可能在sublime文本控制台上显示程序执行输出。



这是我现在必须用崇高文本编译C程序3



c_compile.sublime-build

  {
cmd:[gcc,$ file_name,-o,$ {file_base_name }],
selector:source.c,
shell:false,
working_dir:$ file_path
}

我尝试添加&& ./$ {file_base_name} 像这样:

  {
cmd:[ gcc,$ file_name,-o,$ {file_base_name},&&,./$ {file_base_name}],
selector:source.c ,
shell:false,
working_dir:$ file_path
}

但它给了我这个错误:

  gcc:error:&&文件或目录
[用0.0退出代码1完成]
[cmd:['gcc','Ex1-6.c','-o','Ex1-6','& amp; ;&','./Ex1-6']]
[dir:/ home / admin / Desktop / C / book / chap1]
[路径:/ usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin:/ usr / games:/ usr / local / games]

以下是我正在使用的简单C程序:
$ b Ex1-6.c

  #include< stdio.h> 

main(){
printf(Hello world);
}

我在网上搜索了一个解决方案,但是建议的答案要么只允许编译部件已经在为我工作),或者不起作用。任何想法如何解决这个代码,以编译和运行在崇高的文本3(如果可能)。谢谢

按user2357112建议的编辑#1:

code> shell 至 true

 cmd:[gcc,$ file_name,-o,$ {file_base_name},&&,./$ {file_base_name}] ,
selector:source.c,
shell:true,
working_dir:$ file_path
}



这就是我得到的结果:

  gcc:致命错误:没有输入文件
编译终止。
[以0.0结束,退出代码4]
[cmd:['gcc','Ex1-6.c','-o','Ex1-6','&& ','./Ex1-6']]
[dir:/ home / admin / Desktop / C / book / chap1]
[路径:/ usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin:/ usr / games:/ usr / local / games]

编辑#2如Eugene K建议的那样:

我试着改变cmd来运行程序:

  {
cmd:[./${file_base_name}],
选择器:source.c,
shell:false,
working_dir:$ file_path
}

它成功运行并在控制台上输出一些代码:

  Hello world 
[用0.0退出代码12完成]
[cmd:['./Ex1-6']]
[dir:/ home / amir / Desktop / C / usr / sbin:/ usr / bin:/ sbin:/ bin:/ usr / games:/ usr / local / sbin:/ usr /本地/游戏]

到目前为止,cm d或者编译或者运行,但是不能一起执行,希望可以通过一个命令来编译和运行。



  {
cmd:您是否尝试将单个字符串中的整个命令写出来? [gcc $ file_name -o $ {file_base_name}&& ./${file_base_name}],
selector:source.c,
shell:true,
working_dir:$ file_path
}

我相信(这里是半推测),ST3将第一个参数作为程序将其他字符串作为参数传递。 https://docs.python .org / 2 / library / subprocess.html#subprocess.Popen


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.

解决方案

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

这篇关于如何在崇高的文本3编译和运行C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆