I am currently using the below build system in ST3 for compiling .cpp codes
{
"cmd": ["mingw32-g++.exe", "-c", "$file", "-o", "${file_path}/${file_base_name}.o", "&&",
"mingw32-g++.exe", "-o", "${file_path}/${file_base_name}.exe", "${file_path}/${file_base_name}.o", "&&",
"start","${file_path}/${file_base_name}.exe"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++,source.cpp",
"shell": true
}
The problem I face is, I need to type the inputs one by one into cmd, which is time consuming.
Is there a possibility to somehow tweak the above build system to perform a similar action given below?
On cmd.
${file_base_name}.exe < TestCases.txt > TestAnswers.txt
This is possible in the standard way, which is to use input and output redirection on the command line as outlined in your question itself. However in order for that to work, you need to change how you're defining your build.
Using cmd
and shell
together (as you're doing currently), you specify the command line as a program to run and the arguments that it should get (this is what you're doing now).
What you want to use here is shell_cmd
, in which case you provide a single string that represents the complete command line as you would enter it in a command prompt, which is easier overall.
Without getting into the gritty details of the underlying mechanisms involved, the two are treated and executed differently by the Python runtime, but using shell_cmd
hands your command line directly to the command processor to handle.
An example of a modified build would be this:
{
"shell_cmd": "mingw32-g++.exe \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\" < Input.txt > Answers.txt",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
}
The first thing to note here is that because shell_cmd
is a single string, all of the file names need to be wrapped in "quotes"
to guard against filenames that contain spaces, which is not something you have to do while using cmd
because in that case each argument is a separate string.
Additionally the command line has been cleaned up here a bit because you should be able to compile a file directly to an executable without having to perform a separate linking step if you're using gcc/mingw (if you would prefer, that step is easy enough to add back though).
As defined here, the working directory is set to the path that the file being compiled is stored in, which means that the input file the command is looking for needs to be in that folder and that's where the output file will be created.
If you open the output file, Sublime will reload it every time you run the build (you may or may not be prompted, depending on your preferences).