There are only two ways to get Sublime to execute an external program; use the exec
command, or write a sublime-build
to drive the build. Build systems use the exec
command, and all you’re doing is writing the arguments for the exec
command out, so either way you go you need to know the same thing.
Contrary to what seems to be the opinion of the internet in general (possibly due to several decades old blog posts on the matter), Sublime ships with a C Single File.sublime-build
and C++ Single File.sublime-build
out of the box that will, if you have gcc
installed, build and run a single file C program out of the box.
Builds in Sublime can’t take interactive keyboard input; if you need that, install Terminus, duplicate the built in build, and add in target
and cancel
keys that tell the build to use Terminus (the README on that linked page links to two YouTube videos that describe how to do this).
Frequently these competitive programming situations use build systems that use redirection to send input to the program from a file, and write output to another file. How you do that in a build system is exactly the same way that you would do it if you were doing it in a terminal (except that you would use build variables to specify the name of the file that contains the program). For example (simplistically and untested):
{
"shell_cmd": "gcc \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\" < input.in > output.out",
"selector": "source.c, source.cpp".
}
The text you put in shell_cmd
is literally the command you would enter into the terminal to achieve the same effect.
You may see advice to use cmd
instead of shell_cmd
for this and to set "shell": true
in the build; do not do that, and consider that advice and any other advice that comes from the same source as suspect.