I am using Ubuntu 12.04, and I was wondering, is it possible to automatically run c++ program from terminal? 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. I am using Sublime text 3.
我正在使用Ubuntu 12.04,我在想,是否可以自动从终端运行c++程序?当你不得不在控制台使用构建时,它真的很糟糕,因为有时我在意外中会产生无限循环,并且不得不重新启动令人惊叹的文本。我使用的是崇高的文本3。
5 个解决方案
#1
17
Sublime Text 3 includes two build systems you might be interested in: C++ and Make. The C++.sublime-build
file is as follows:
卓越的文本3包括两个您可能感兴趣的构建系统:c++和Make。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++
. You can now use CtrlB to run the build (top command), or CtrlShiftB to run the Run
variant.
要使用它,请访问工具->构建系统并选择c++。现在,您可以使用ctrl lb来运行构建(top命令),或者使用ctrl lshiftb来运行该运行的变体。
#2
7
{
"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
它可以在终端运行,也可以从键盘输入数据。
#3
1
I think the top answer does not achieve what the OP want to achieve. The OP wanted to know how to execute the current file in a terminal.
我认为上面的答案并没有达到OP想要达到的目标。OP想知道如何在终端中执行当前文件。
@Flycode's setting does not work for me. I am using CentOS 7 with Sublime Text 3. Since people may use different terminal emulators, so I list some different options for different terminals.
@Flycode的设置对我不起作用。我用的是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.
下面的设置是在上面的环境中测试的,并且效果很好。我不能保证他们会在其他环境下工作。
Option 1: GNOME Terminal
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自动关闭的详细讨论,请参阅本文。
Option 2: XTerm
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窗口自动关闭的文章。
Option 3: Konsole
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窗口。
#4
0
On Mac, I use fswatch (I'm sure there's something like it on Linux) to automatically build & run the testcase on save.
在Mac上,我使用fswatch(我确定Linux上有类似的东西)来自动构建和运行保存的testcase。
#5
0
Here is my configuration to compile and run C++ programs. Program takes input from file 'input.txt' and prints output to 'output.txt'.Both the files present in current working directory.
OS: ubuntu 16
sublime 3
-> "Tools > Build System > new Build System" and copy following setting
下面是编译和运行c++程序的配置。程序从文件输入中获取输入。并将输出输出到“output.txt”。这两个文件都存在于当前工作目录中。操作系统:ubuntu16卓越3 ->“工具>构建系统>新构建系统”,并复制如下设置。
{
"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 \"'",
}
] }
#1
17
Sublime Text 3 includes two build systems you might be interested in: C++ and Make. The C++.sublime-build
file is as follows:
卓越的文本3包括两个您可能感兴趣的构建系统:c++和Make。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++
. You can now use CtrlB to run the build (top command), or CtrlShiftB to run the Run
variant.
要使用它,请访问工具->构建系统并选择c++。现在,您可以使用ctrl lb来运行构建(top命令),或者使用ctrl lshiftb来运行该运行的变体。
#2
7
{
"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
它可以在终端运行,也可以从键盘输入数据。
#3
1
I think the top answer does not achieve what the OP want to achieve. The OP wanted to know how to execute the current file in a terminal.
我认为上面的答案并没有达到OP想要达到的目标。OP想知道如何在终端中执行当前文件。
@Flycode's setting does not work for me. I am using CentOS 7 with Sublime Text 3. Since people may use different terminal emulators, so I list some different options for different terminals.
@Flycode的设置对我不起作用。我用的是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.
下面的设置是在上面的环境中测试的,并且效果很好。我不能保证他们会在其他环境下工作。
Option 1: GNOME Terminal
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自动关闭的详细讨论,请参阅本文。
Option 2: XTerm
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窗口自动关闭的文章。
Option 3: Konsole
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窗口。
#4
0
On Mac, I use fswatch (I'm sure there's something like it on Linux) to automatically build & run the testcase on save.
在Mac上,我使用fswatch(我确定Linux上有类似的东西)来自动构建和运行保存的testcase。
#5
0
Here is my configuration to compile and run C++ programs. Program takes input from file 'input.txt' and prints output to 'output.txt'.Both the files present in current working directory.
OS: ubuntu 16
sublime 3
-> "Tools > Build System > new Build System" and copy following setting
下面是编译和运行c++程序的配置。程序从文件输入中获取输入。并将输出输出到“output.txt”。这两个文件都存在于当前工作目录中。操作系统:ubuntu16卓越3 ->“工具>构建系统>新构建系统”,并复制如下设置。
{
"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 \"'",
}
] }