This past semester I took an operating systems class and really enjoyed learning how to create a dynamic Makefile. My previous experience with Makefiles had been cursory, and usually involved copy pasting the example from the lecture slides. Without a real template for most of the code we wrote in this class, I was more or less on my own for the entire OS codebase.
Since we knew this project was going to involved over 10,000 lines of code, I wanted to write a Makefile that would easily support the folowing things:
- Testing
- Nested Directories
- Different Executables
While other groups placed their c and header files in a single directory, internal quality was a sticking point for me. Especially since there were four groupmembers, having a clean, understandable codebase was something I wanted to strictly enforce.
Differing from my previous computer science classes, we were allowed and encouraged to look to outside resources/textbooks for information. After browsing through several StackOverflow and online forum posts, I couldn't find a file that would complete everything I needed in a clear, concise way. With my Safari Books subscription, the first book available was Managing Projects with GNU Make, 3rd Edition. AFter reading it through, it offered a fundamental understanding of makefiles, but there was no clear solutions to build from.
It was an obscure post on linux forums that pointed out using shell scripts in makefiles to copy directories/files was very easy. This gave me a baseline and insight on how to manipulate files in Make. Combining this with what I learned from the textbook, I was able to create a Makefile that finally did everything I needed. While this Makefile was a significant time investment for so few lines of code, I know I'll be able to reuse this template or easily modify it in the future.
I've attached the file below. Feel free to use this for your personal projects that are on the larger side.
Makefile 😍