ben_dover's blog

By ben_dover, 5 weeks ago, In English,

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 round619/a will generate binary round619/a from source round619/a.cpp, and if we copied the makefile into our source's folder we can cd into the folder and simply type make a. Unfortunately the target does not tab-complete in the shell. This is only marginally easier than compiling normally but it is an option.

  • Vote: I like it
  • +16
  • Vote: I do not like it