/dev/shm

Some Tricks for Make

Directory Switching

I’m currently taking an operating system class where we work on the OS/161 operating system. Working on it is fine, but building the operating system became a pain for one reason, and one reason only: the makefile was in a subdirectory of the operating system.

This was a pain until I figured out that make has the -C option, which changes directory before calling the build. This is awesome, because now VIM can process the output of make and I can use quickfix, as mentioned in a previous post, to jump between errors. Now, instead of just typing :make, I type :make -C dir and everything works as expected.

Building Dependencies

Adding all of your files to your Makefile is a pain. Fortunately, if you’re working with GCC, you can use a few tricks to make this easier on yourself.

GCC provides a wonderful option: -MMD. This option creates small makefile pieces that contain dependencies for every .c file in your directory. This is great, because make actually knows how to use this to do your compiling for you!

You can use the option to generate dependency files in your makefile with a depend target. Then, to compile with make, all your targets need to do is include the files generated by gcc! Make sure to clean up these files also when you run your clean target.

Posted on 08 October 2011.