我想在Ubuntu 14.04的Sublime Text 3中编译和运行C程序。目前,使用gcc编译程序并通过执行命令(见下面的代码)在Sublime Text 3中进行编译,但我想知道是否可以使程序的执行输出也出现在Sublime Text控制台中。
以下是我目前使用Sublime Text 3编译C程序的内容:
c_compile.sublime-build
但是它给我返回了这个错误:
我在网上搜索了解决方案,但所提供的答案要么只允许编译(这部分已经对我有效),要么无法工作。有没有办法修复这段代码,以便在Sublime Text 3中编译和运行呢?谢谢。 根据user2357112的建议进行编辑 #1: 将
以下是我目前使用Sublime Text 3编译C程序的内容:
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: &&: 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]
以下是我正在处理的简单C程序:
Ex1-6.c
#include <stdio.h>
main(){
printf("Hello world");
}
我在网上搜索了解决方案,但所提供的答案要么只允许编译(这部分已经对我有效),要么无法工作。有没有办法修复这段代码,以便在Sublime Text 3中编译和运行呢?谢谢。 根据user2357112的建议进行编辑 #1: 将
shell
更改为true
后:{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}","&&","./${file_base_name}"],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}
这是我得到的结果:
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]
根据Eugene K的建议修改#2:
我尝试仅使用cmd运行程序:
{
"cmd" : ["./${file_base_name}"],
"selector" : "source.c",
"shell":false,
"working_dir" : "$file_path"
}
代码成功运行并在控制台上打印输出:
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]
到目前为止,cmd 命令要么编译,要么运行,但不能同时进行,希望能够做些改进,使其可以用单个命令既编译又运行。
&&
是一个 shell 的东西。这可能不是一个好主意(我不知道如何使用 Sublime Text 3),但设置"shell":true
可能会使事情正常工作。 - user2357112fatal error: no input files, compilation terminated.
而不是gcc: error: &&
。所以我猜你关于 shell 的事情是正确的,但还是不起作用 :/ - CMPScmd:["./${file_base_name}"]
,现在第一个命令被覆盖了,文件只是运行而没有编译,这显示出一些有希望的解决方案。感谢您的建议。 - CMPSgcc
命令组和filename
部分之间加入exit
或类似的东西可能会奏效。 - Utkan Gezer