如何在崇高文本3中编译和运行C?

CMP*_*MPS 37 c gcc sublimetext3 ubuntu-14.04

我想在ubuntu 14.04上的sublime text 3中编译和运行C程序.目前正在使用gcc编译程序,使用执行命令的sublime text 3(参见下面的代码),但我想知道是否有可能让程序执行输出也出现在sublime文本控制台上.

这是我目前用sublime text 3编译C程序的方法

c_compile.sublime建造

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}"],
"selector" : "source.c",
"shell":false,
"working_dir" : "$file_path"
}
Run Code Online (Sandbox Code Playgroud)

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

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}","&&","./${file_base_name}"],
"selector" : "source.c",
"shell":false,
"working_dir" : "$file_path"
}
Run Code Online (Sandbox Code Playgroud)

但它给了我这个错误:

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]
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的简单C程序:

Ex1-6.c

#include <stdio.h>

main(){
    printf("Hello world");
}
Run Code Online (Sandbox Code Playgroud)

我在网上搜索了一个解决方案,但建议的答案要么只允许编译(这部分已经在为我工作),或者不起作用.知道如何修复此代码,以便在sublime文本3中编译和运行(如果可能).谢谢

根据user2357112的建议编辑#1:

更改shelltrue:

{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}","&&","./${file_base_name}"],
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

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]
Run Code Online (Sandbox Code Playgroud)

根据Eugene K的建议编辑#2:

我尝试更改cmd以仅运行程序:

{
"cmd" : ["./${file_base_name}"],
"selector" : "source.c",
"shell":false,
"working_dir" : "$file_path"
}
Run Code Online (Sandbox Code Playgroud)

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

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]
Run Code Online (Sandbox Code Playgroud)

到目前为止,cmd要么编译要么运行,但不能同时进行,希望可以通过单个命令进行编译和运行.

erb*_*dge 43

您是否尝试过用单个字符串写出整个命令?

{
"cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path"
}
Run Code Online (Sandbox Code Playgroud)

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

  • 如果它为将来的编码器节省了一些麻烦,我只想注意你应该将它放在构建系统配置文件中,方法是转到工具>构建系统>新构建系统而不是在首选项配置文件中(首选项>设置 - 更多>语法特定). (6认同)

小智 26

对于实现Run菜单命令的sublime构建系统:

  • 转到工具 - >构建系统 - >新构建系统...

要么

  • 创建一个文件 ~/.config/sublime-text-3/Packages/User/GCC.sublime-build

插入此:

{
"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}"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

*此示例使用GCC编译器.随意替换gcc您选择的编译器.