Sublime文本3-编译程序并在终端上运行
Sublime text 3 - compile program and run in terminal
我使用的是Ubuntu 12.04,我想知道,是否可以从终端自动运行c++程序?当你不得不使用内置控制台时,这真的很糟糕,因为有时我会意外地进行无限循环,并且必须重新启动精彩的文本才能再次工作。我正在使用Sublime文本3。
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}""
}
]
}
要使用它,请转到Tools -> Build System
并选择C++
。现在,您可以使用CtrlB来运行构建(top命令),或者使用CtrlShiftB来运行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"'"]
}
]
}
它可以在终端中运行,并从键盘输入数据
我认为接受的答案并没有实现OP想要实现的目标OP想知道如何在终端中执行当前文件。
@Flycode的设置对我不起作用。我正在使用带有Sublime Text 3的CentOS 7。由于人们可能使用不同的终端模拟器,所以我列出了不同终端的不同设置。
注意
以下设置在上述环境中进行了测试,效果良好。我不能保证他们会在其他环境中工作。如果它对你不起作用,请告诉我。
选项1:GNOME终端
您可以使用以下设置,
{
"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终端会自动关闭执行窗口,上面的命令
"shell_cmd": "gnome-terminal -e 'bash -c "${file_path}/${file_base_name};exec bash "'"
以确保我们可以看到执行结果。有关如何防止gnome终端自动关闭的详细讨论,请参阅这篇SO文章。
选项2:XTerm
您可以使用以下设置(为了简洁起见,我省略了一些设置)
{ // 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}",
}
]
}
请参阅这篇关于防止xterm窗口自动关闭的SO文章。
选项3:Konsole
您可以使用以下设置,
{ // same stuff as option 1
"variants":
[
{
"name": "Run",
"shell_cmd": "konsole --hold -e ${file_path}/./${file_base_name}",
}
]
}
请参阅此处和此处讨论在执行程序后举行konsole窗口。
工具>>构建系统>>新建构建系统然后粘贴并按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
}
]
}
通过此构建,您可以通过按ctrl+shift+B直接从子me运行C/C++程序。
它允许用户在运行时进行输入。
当您直接运行时,它还可以通过在终端窗口上显示错误来帮助调试
{
"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上也有类似的东西)来自动构建&保存时运行测试用例。
以下是我编译和运行C++程序的配置。程序从文件"input.txt"获取输入,并将输出打印到"output.txt"。这两个文件都存在于当前工作目录中
OS: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 "'",
}
] }
在目录Tools>>Build System>>New Build System中。创建新文件。现在也可以输入。
{
"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...'"]
}
]
}
- 如何在linux终端中同时编译和运行c++代码
- 使用 ubuntu 终端在 c++ 上运行线程类的问题
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- 如何在不丢失输出的情况下杀死/停止终端中正在运行的进程?
- 在终端中运行可执行文件与在Mac查找器中打开
- 终端命令在程序运行后自动打开图形
- 使用 (cin) 用户输入将其粘贴到 std::system 中,并在另一个终端中运行带有输入的命令
- 在集成终端上运行vscode lldb调试器时,如何获取程序的输出?
- 如何在调试模式下通过终端运行可执行的 c++?
- 如何在Visual Studio Code的终端内运行C++代码?
- 如何从终端运行用 eclipse 编写的 c++ 程序
- 我想在终端窗口中运行我的 CLion 程序
- 从 eclipse IDE 运行工作正常,但在从终端运行时终止
- 在终端运行C 程序
- 从终端运行C++程序.在同一终端窗口中获取输出
- 使用sublime-build时不能运行c++程序,从终端运行时工作正常
- 在cygwin终端运行时显示不存在的文件夹
- NetBeans在外部终端运行
- 用Xcode用c++写一个文件,用终端运行
- C++代码仅在程序从终端运行时执行