C语言 如何在 sublime text 3 中编译和运行 C?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24225343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile and run C in sublime text 3?
提问by CMPS
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.
我想在 ubuntu 14.04 上以 sublime text 3 编译和运行 C 程序。目前正在使用执行命令的 sublime text 3 使用 gcc 编译程序(见下面的代码),但我想知道是否有可能让程序执行输出也出现在 sublime text 控制台上。
Here's what I currently have to compile C program with sublime text 3
这是我目前必须用 sublime text 3 编译 C 程序的内容
c_compile.sublime-build
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:
我试过这样添加&& ./${file_base_name}:
{
"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:
这是我正在使用的简单 C 程序:
Ex1-6.c
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
我在网上搜索了一个解决方案,但建议的答案要么只允许编译(这部分已经对我有用),要么不起作用。知道如何修复此代码以便在 sublime text 3 中编译和运行(如果可能)。谢谢
Edit #1 as suggested by user2357112:
按照 user2357112 的建议编辑 #1:
After changing shellto true:
更改shell为 后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:
按照 Eugene K 的建议编辑 #2:
I tried changing cmd to run the program only:
我尝试将 cmd 更改为仅运行程序:
{
"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.
到目前为止,cmd 要么编译要么运行,但不能同时执行,希望可以做些什么来使其编译并使用单个命令运行。
回答by erbridge
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
我相信(这里是半推测),ST3 将第一个参数作为“程序”,并将其他字符串作为“参数”传入。https://docs.python.org/2/library/subprocess.html#subprocess.Popen
回答by Gonen
For a sublime build system implementing the Run menu command :
对于实现运行菜单命令的 sublime 构建系统:
- Go to Tools->Build System->New Build System...
- 转到工具->构建系统->新建构建系统...
Or
或者
- Create a file
~/.config/sublime-text-3/Packages/User/GCC.sublime-build
- 创建文件
~/.config/sublime-text-3/Packages/User/GCC.sublime-build
And insert this:
并插入这个:
{
"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}"
}
]
}
*This example uses the GCC compiler. Feel free to replace gccwith the compiler of your choice.
*此示例使用 GCC 编译器。随意替换gcc为您选择的编译器。
回答by rock321987
We can compile the code of C in Sublime Text and can print some value or strings but it does notaccept input from the user. (Till I know... I am sure about compiling but not about output from given input.) If you are using Windows you have to set the environment variables for Sublime Text and GCC compiler.
我们可以在 Sublime Text 中编译 C 的代码,可以打印一些值或字符串,但它不接受用户的输入。(直到我知道......我确定编译但不是给定输入的输出。)如果您使用的是 Windows,则必须为 Sublime Text 和 GCC 编译器设置环境变量。
回答by Sayan Sil
After a rigorous code-huntingsession over the internet, I finally came up with a solution which lets you compile + runyour C code "together at once", in C99, in a dedicated terminal window. I know, a few people dont like C99. I dont like a few people either.
在互联网上进行了严格的代码搜索会议后,我终于想出了一个解决方案,它可以让您在C99中的专用终端窗口中“同时”编译和运行C 代码。我知道,有些人不喜欢C99。我也不喜欢几个人。
In most of the cases Sublime compiles and runsthe code, but in C90 or a lesser version. So if you specifically want it to be C99, this is the way to go.
在大多数情况下,Sublime编译并运行代码,但在 C90 或更低版本中。所以如果你特别希望它是 C99,这是要走的路。
NOTE: Btw, I did this on a Windows machine, cannot guarantee for others! It probably won't work there.
注意:顺便说一句,我是在 Windows 机器上做的,不能保证其他人!它可能不会在那里工作。
1. Create a new build system in Sublime:Tools > Build System > New Build System...
1. 在 Sublime 中创建一个新的构建系统:Tools > Build System > New Build System...
2. A new file called untitled.sublime-buildwould be created.
2.untitled.sublime-build将创建一个名为的新文件。
Most probably, Sublime will open it for you.
很有可能,Sublime 会为你打开它。
If not, go to Preferences > Browse Packages > User
如果没有,请转到首选项 > 浏览包 > 用户
If the file untitled.sublime-buildis there, then open it,
if it isn't there, then create it manuallyand open it.
如果该文件untitled.sublime-build存在,则打开它,如果它不存在,则手动创建并打开它。
3. Copy and paste the given below code in the above mentioned untitled.sublime-buildfile and save it.
3.将下面给出的代码复制并粘贴到上述untitled.sublime-build文件中并保存。
{
"windows":
{
"cmd": ["gcc","-std=c99" ,"$file_name","-o", "${file_base_name}.exe", "-lm", "-Wall", "&","start", "${file_base_name}.exe"]
},
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path",
}
Close the file. You are almost done!
关闭文件。你快完成了!
4. Finally rename your file from untitled.sublime-buildto myC.sublime-build, or you might as well show your creativity here. Just keep the file extension same.
4. 最后将您的文件从 重命名untitled.sublime-build为myC.sublime-build,或者您也可以在这里展示您的创造力。只需保持文件扩展名相同。
5. Finallyset the current Build Systemto the filename which you wrote in the previous step. In this case, it is myC
5. 最后将当前构建系统设置为您在上一步中编写的文件名。在这种情况下,它是myC
Voila !
Compile + Runyour C code using C99 by Tools > Build, or by simply pressing Ctrl + B
瞧!通过Tools > Build使用 C99编译 + 运行C 代码,或者只需按Ctrl + B
回答by Sergei Nazarenko
The best way would be just to use a Makefile for your project and ST3 will automatically detect build system for your project. For example. If you press shift + ctrl/cmd +B you will see this:

最好的方法是为您的项目使用 Makefile,ST3 将自动检测您项目的构建系统。例如。如果你按 shift + ctrl/cmd +B 你会看到这个:

回答by Maks
Instruction is base on the "icemelon" post. Link to the post:
说明基于“icemelon”帖子。链接到帖子:
how-do-i-compile-and-run-a-c-program-in-sublime-text-2
how-do-i-compile-and-run-ac-program-in-sublime-text-2
Use the link below to find out how to setup enviroment variable on your OS:
使用以下链接了解如何在您的操作系统上设置环境变量:
The instruction below was tested on the Windows 8.1 system and Sublime Text 3 - build 3065.
下面的指令在 Windows 8.1 系统和 Sublime Text 3 - build 3065 上进行了测试。
1) Install MinGW. 2) Add path to the "MinGW\bin" in the "PATH environment variable".
1)安装MinGW。2)在“PATH环境变量”中添加“MinGW\bin”的路径。
"System Properties -> Advanced -> Environment" variables and there update "PATH' variable.
“系统属性 -> 高级 -> 环境”变量并更新“PATH”变量。
3) Then check your PATH environment variable by the command below in the "Command Prompt":
3)然后通过“命令提示符”中的以下命令检查您的PATH环境变量:
echo %path%
4) Add new Build System to the Sublime Text.
4) 将新的构建系统添加到 Sublime Text。
My version of the code below ("C.sublime-build").
我的以下代码版本(“C.sublime-build”)。
link to the code:
代码链接:
// Put this file here:
// "C:\Users\[User Name]\AppData\Roaming\Sublime Text 3\Packages\User"
// Use "Ctrl+B" to Build and "Crtl+Shift+B" to Run the project.
// OR use "Tools -> Build System -> New Build System..." and put the code there.
{
"cmd" : ["gcc", "$file_name", "-o", "${file_base_name}.exe"],
// Doesn't work, sublime text 3, Windows 8.1
// "cmd" : ["gcc $file_name -o ${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path",
// You could add path to your gcc compiler this and don't add path to your "PATH environment variable"
// "path" : "C:\MinGW\bin"
"variants" : [
{ "name": "Run",
"cmd" : ["${file_base_name}.exe"]
}
]
}
回答by Akansh
The latest build of the sublime even allows the direct command instead of double quotes. Try the below code for the build system
sublime 的最新版本甚至允许直接命令而不是双引号。为构建系统尝试以下代码
{
"cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path",
}
回答by DucNgo
Are you using sublime text on linux? I got the same problem and it was solved! Here is my c.sublime-build:
你在 linux 上使用 sublime text 吗?我遇到了同样的问题,并已解决!这是我的 c.sublime-build:
{
"shell_cmd" : "gcc $file_name -o $file_base_name && ./$file_base_name",
"selector" : "source.c",
"shell":true,
"working_dir" : "$file_path"
}
回答by thebluebloo
The code that worked for me on a Windows 10 machine using Sublime Text 3
在使用 Sublime Text 3 的 Windows 10 机器上对我来说有效的代码
{
"cmd" : "gcc $file_name -o ${file_base_name}",
"selector" : "source.c",
"shell" : true,
"working_dir" : "$file_path",
"variants":
[
{
"name": "Run",
"cmd": "${file_base_name}"
}
]
}
回答by Neil
In Sublime Text 3....Try changing the above code to this, note the addition of "start".....
在 Sublime Text 3....尝试将上面的代码改成这样,注意添加了“start”.....
"variants" : [
“变体”:[
{ "name": "Run",
"cmd" : ["start", "${file_base_name}.exe"]
}

