Sublime文本3-编译程序并在终端上运行

Sublime text 3 - compile program and run in terminal

本文关键字:终端 运行 文本 编译程序 Sublime      更新时间:2023-10-16

我使用的是Ubuntu 12.04,我想知道,是否可以从终端自动运行c++程序?当你不得不使用内置控制台时,这真的很糟糕,因为有时我会意外地进行无限循环,并且必须重新启动精彩的文本才能再次工作。我正在使用Sublime文本3。

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}""
}
]
}

要使用它,请转到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...'"]
}
]

}