C++ Sublime text 3 - 编译程序并在终端中运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21196077/
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
Sublime text 3 - compile program and run in terminal
提问by user2648841
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++ 程序?当您必须在控制台中使用构建时,这真的很糟糕,因为有时我会意外地进行无限循环,并且必须重新启动 sublime text 才能再次工作。我正在使用 Sublime 文本 3。
回答by MattDMo
Sublime Text 3 includes two build systems you might be interested in: C++ and Make. The C++.sublime-build
file is as follows:
Sublime Text 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 CtrlBto run the build (top command), or CtrlShiftBto run the Run
variant.
要使用它,请转到Tools -> Build System
并选择C++
。您现在可以使用CtrlB来运行构建(top 命令)或CtrlShiftB运行Run
变体。
回答by Flycode
{
"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
它可以在终端中运行并从键盘输入数据
回答by jdhao
I think the accepted 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 different settings 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. Let me know if it does not work for you.
以下设置在上述环境下测试,运行良好。我不能保证它们会在其他环境中工作。如果它不适合您,请告诉我。
Option 1: GNOME Terminal
选项 1:GNOME 终端
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 postfor a detailed discussion about how to prevent gnome-terminal from closing automatically.
以这种方式使用以确保我们可以看到执行结果。有关如何防止 gnome-terminal 自动关闭的详细讨论,请参阅此 SO 帖子。
Option 2: XTerm
选项 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 postabout preventing xterm window from closing automatically.
请参阅有关防止 xterm 窗口自动关闭的SO 帖子。
Option 3: Konsole
选项 3:控制台
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 hereon discussion to hold konsole windows after excuting the program.
有关在执行程序后保持 konsole 窗口的讨论,请参见此处和此处。
回答by HimEl
Tools >> Build System >> New Build System Then paste this And press Ctrl+S to Save file. 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 + S保存文件。现在转到工具>>构建系统>>选择您的文件>>现在编写您的代码>>按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
}
]
}
}
回答by akhil
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\""
],
}
]
}
回答by TARUN SAINI
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...'"]
}
]
}
}
回答by Eugene
回答by snehm
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++ 程序的配置。程序从文件“input.txt”获取输入并将输出打印到“output.txt”。这两个文件都存在于当前工作目录中。
操作系统:ubuntu 16
sublime 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 \"'",
}
] }