zettelkasten

Makefiles

Last updated: 1/9/2025

But writing the lines to compile all of my source files takes so long 😩 Makefile has got you covered 😎

Basically, it is a way to "make" your project based on seeing which files are older/newer than other files. It uses rules like this:

Pasted image 20230717092041
Pasted image 20230717092041
Which, when used, looks like this:
Pasted image 20230717092109
Pasted image 20230717092109
So whenever src/main.cc is newer than main.out, then this will be run!

Since Makefile is a programming language, you can use variables to simplify things!

Pasted image 20230717092436
Pasted image 20230717092436

But the power of makefiles do not end here! You can make phony targets (using .PHONY) in order to use make to type out and keep track of long-ass commands for you:

Pasted image 20230717092951
Pasted image 20230717092951

Here are some cool variables:

Pasted image 20230717093347
Pasted image 20230717093347
In addition to this, you can use pattern matching by using a % sign. Ex. if you want to "tie" headers and source files together you can do something like:

c++
./src/%.cc: ./includes/%.cc
%@

Which will mark all of the source files as modified whenever you change any of the headers, which is SUPER useful for keeping everything up-to-date

See Also

  1. compilation-and-execution-in-c
  2. [[computer-science]]