Sublime Forum

How to run cpp code in sublime test 4 without making any new build system

#1

0

I am going to give one offline programming contest and i am going to use sublime editor in that contest. But the issue is that in sublime we first need to make build system to run cpp code. And my brain is too weak to remember whole build system also if i made some mistake while writing build system contest will be ruin.

So i want to ask for a simple way to run cpp code in sublime wihout making build system. obviously one way to run code my terminal but i will also have to stress test code and doing that from terminal is not possible and i personally dont like running code from terminal.

Any help will be appreciated. Thanks in advance.

0 Likes

#2

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.

1 Like