If you are like me you probably have a favorite set of compiler flags, and you use Ctrl-R or press up a few times in the terminal to get the command used for compiling, and then you change the source file and binary name in the command to the right names. (Or you are not like me and hit the compile button in your IDE.) This works fine but always searching for the command and changing the file names is a little annoying.
The standard solution for doing the same compilation commands over and over is make, but usually makefiles are for projects with a few predetermined targets (generating one main binary), while competition programming is a unique environment where we need to create many binaries, one from each source file. I recently discovered that makefiles can pattern match (yes I am a make noob) so we can compile arbitrary files. In short:
% : %.cpp
$$$(CXX) $$$(CXXFLAGS) -o $$$@ $$$<
(There should only be one dollar sign in each term. For some reason CF blogs render a single dollar sign in block code as 3.)
To use the makefile we simply provide the path to the program without the file extension. make -a
will generate binary a
from source a.cpp
, and use -f ../Makefile
if assuming the Makefile is in the root directory one level above. Unfortunately the target does not tab-complete in the shell. This is only marginally easier than compiling normally but it is an option.