[英]Sublime text 3 - compile program and run in terminal
I am using Ubuntu 12.04, and I was wondering, is it possible to automatically run c++ program from terminal?我使用的是 Ubuntu 12.04,我想知道是否可以从终端自动运行 C++ 程序? It really sucks when you have to use build in console because sometimes I make infinite loops by accident and have to restart sublime text to work again.
当您必须在控制台中使用构建时,这真的很糟糕,因为有时我会意外地进行无限循环,并且必须重新启动 sublime text 才能再次工作。 I am using Sublime text 3.
我正在使用 Sublime 文本 3。
Sublime Text 3 includes two build systems you might be interested in: C++ and Make. Sublime Text 3 包含两个您可能感兴趣的构建系统:C++ 和 Make。 The
C++.sublime-build
file is as follows: C++.sublime-build
文件如下:
{
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
To use it, go to Tools -> Build System
and select C++
.要使用它,请转到
Tools -> Build System
并选择C++
。 You can now use Ctrl B to run the build (top command), or Ctrl Shift B to run the Run
variant.您现在可以使用Ctrl B运行构建(top 命令),或使用Ctrl Shift B运行
Run
变体。
{
"cmd": ["g++", "$file", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cxx, source.cpp",
"variants":
[
{
"name": "Run",
"shell": true,
"cmd": ["gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};echo;echo; echo Press ENTER to continue; read line;exit; exec bash\"'"]
}
]
}
It can run in terminal and input data from keyboard它可以在终端中运行并从键盘输入数据
I think the accepted answer does not achieve what the OP want to achieve.我认为接受的答案没有实现 OP 想要实现的目标。 The OP wanted to know how to execute the current file in a terminal .
OP 想知道如何在终端中执行当前文件。
@Flycode's setting does not work for me. @Flycode 的设置对我不起作用。 I am using CentOS 7 with Sublime Text 3. Since people may use different terminal emulators, so I list different settings for different terminals.
我使用的是 CentOS 7 和 Sublime Text 3。由于人们可能使用不同的终端模拟器,所以我列出了不同终端的不同设置。
Note笔记
The following settings are tested on the above environment and works well.以下设置在上述环境下测试,运行良好。 I can not guarantee that they will work on other environments.
我不能保证它们会在其他环境中工作。 Let me know if it does not work for you.
如果它不适合您,请告诉我。
You can use the following setting,您可以使用以下设置,
{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"shell": true,
"working_dir": "${file_path}",
"selector": "source.c++, source.cxx, source.cpp, source.cc",
"variants":
[
{
"name": "Run",
"shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};exec bash \"'",
}
]
}
gnome-terminal will automatically close the execution window, the above command gnome-terminal 会自动关闭执行窗口,上面的命令
"shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};exec bash \"'"
is used that way to make sure we can see the execution result.以这种方式使用以确保我们可以看到执行结果。 See this SO post for a detailed discussion about how to prevent gnome-terminal from closing automatically.
有关如何防止 gnome-terminal 自动关闭的详细讨论,请参阅此 SO 帖子。
You can use the following setting (For brevity, I leave out some settings)您可以使用以下设置(为简洁起见,我省略了一些设置)
{ // same stuff as option 1
"variants":
[
{
"name": "Run",
//use this if you want to input other command after programm execution
"shell_cmd": "xterm -e '${file_path}/${file_base_name}; bash'",
//or you can use the below setting if you just want to execute this program
// "shell_cmd": "xterm -hold -e ${file_path}/${file_base_name}",
}
]
}
See this SO post about preventing xterm window from closing automatically.请参阅有关防止 xterm 窗口自动关闭的SO 帖子。
You can use the following setting,您可以使用以下设置,
{ // same stuff as option 1
"variants":
[
{
"name": "Run",
"shell_cmd": "konsole --hold -e ${file_path}/./${file_base_name}",
}
]
}
See here and here on discussion to hold konsole windows after excuting the program.有关在执行程序后保持 konsole 窗口的讨论,请参见此处和此处。
Tools >> Build System >> New Build System Then paste this And press Ctrl+S to Save file.工具>>构建系统>>新建构建系统然后粘贴这个并按Ctrl + S保存文件。 Now Goto Tools >> Build System >> Select your file >> Now write your code >> Press Ctrl+B >> Select Run in Terminal for Build and run your code
现在转到工具>>构建系统>>选择您的文件>>现在编写您的代码>>按Ctrl + B >>选择在终端中运行以进行构建并运行您的代码
{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"shell": true,
"working_dir": "${file_path}",
"selector": "source.c++, source.cpp, source.cc, source.cxx",
"variants":
[
{
"name": "Run in Terminal",
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" && gnome-terminal -e 'bash -c \"${file_path}/${file_base_name}&& echo && echo Press ENTER to continue && read line && exit\"'", // for gnome-terminal
}
]
} }
Through this build you can directly run you C/C++ programmes from subime by pressing ctrl+shift+B.通过这个构建,你可以通过按 ctrl+shift+B 直接从 subime 运行你的 C/C++ 程序。
It allows user input during run time.它允许用户在运行时输入。
It also helps in debugging by showing errors on terminal window as your terminal shows you when you run directly through it.它还可以通过在终端窗口上显示错误来帮助调试,因为当您直接通过它运行时终端会向您显示。
{
"cmd": "g++ \"${file}\" -o \"${file_path}\\\\${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c,source.c++,source.cpp",
"shell":true,
"variants": [
{
"name": "Run",
"cmd" : ["gnome-terminal -- bash -c \"g++ $file_name ;echo -------------output--------------; ./a.out;echo;echo; echo Press ENTER to continue; read line;exit; exec bash\""
],
}
]
}
在 Mac 上,我使用fswatch (我确定在 Linux 上有类似的东西)在保存时自动构建和运行测试用例。
Here is my configuration to compile and run C++ programs.这是我编译和运行 C++ 程序的配置。 Program takes input from file 'input.txt' and prints output to 'output.txt'.Both the files present in current working directory.
程序从文件“input.txt”获取输入并将输出打印到“output.txt”。这两个文件都存在于当前工作目录中。
OS: ubuntu 16操作系统:Ubuntu 16
sublime 3崇高 3
-> "Tools > Build System > new Build System" and copy following setting ->“工具>构建系统>新构建系统”并复制以下设置
{
"shell_cmd": "g++ -std=c++11 -Wall \"${file}\" -o \"${file_path}/${file_base_name}\" ",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"shell": true,
"working_dir": "${file_path}",
"selector": "source.c++, source.cxx, source.cpp, source.cc",
"variants":
[
{
"name": "Run",
"shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name} < input.txt > output.txt \"'",
}
] }
In directory Tools >> Build System >> New Build System.在目录工具>>构建系统>>新建构建系统中。 create new file.
创建新文件。 Now input can also be give.
现在输入也可以给出。
{ {
"cmd": ["g++", "-Wall", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["x-terminal-emulator", "-e", "bash", "-c", "g++ -Wall -std=c++11 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'; read -p 'Press any key to continue...'"]
}
]
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.