我正在使用Ubuntu 12.04,我想知道,是否有可能从terminal自动运行c + +程序? 当你不得不在控制台中使用build的时候真的很糟糕,因为有时我偶然会发生无限循环,必须重新启动sublime文本才能重新工作。 我正在使用崇高的文字3。
崇高的文本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}\"" } ] }
要使用它,请转至Tools -> Build System
然后选择C++
。 现在可以使用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\"'"] } ] }
它可以在终端运行并从键盘输入数据
在Mac上,我使用fswatch (我确定在Linux上有类似的东西)来自动构建并运行测试用例。
我认为最好的答案并不能实现OP想要达到的目标。 OP想知道如何在终端中执行当前文件。
@ Flycode的设置不适合我。 我使用的是带有Sublime Text 3的CentOS 7.由于人们可能会使用不同的终端仿真器,所以我列出了不同终端的不同选项。
注意
以上设置在上述环境中进行了测试,效果良好。 我不能保证他们会在其他环境下工作。
您可以使用以下设置,
{ "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会自动关闭执行窗口,执行上面的命令
"shell_cmd": "gnome-terminal -e 'bash -c \"${file_path}/${file_base_name};exec bash \"'"
用这种方式来确保我们可以看到执行结果。 看到这个SO帖子详细讨论如何防止gnome终端自动关闭。
您可以使用以下设置(为了简洁起见,我省略了一些设置)
{ // 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}", } ] }
看到这个SO帖子关于防止xterm窗口自动关闭。
您可以使用以下设置,
{ // same stuff as option 1 "variants": [ { "name": "Run", "shell_cmd": "konsole --hold -e ${file_path}/./${file_base_name}", } ] }
查看这里和这里的讨论,以便在完成程序后保存konsole窗口。
这是我的配置来编译和运行C ++程序。 程序从文件“input.txt”获取输入,并将输出打印到“output.txt”。两个文件都出现在当前工作目录中。
操作系统:Ubuntu 16
崇高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 \"'", } ] }