Try   HackMD

How to Write Your Own Makefile?

This article is my study note for writing Makefile for C/C++ programs. In the future, I will extend Makefile techniques to my projects in Python or other programming languages.

TODO List

  • Compile in parallel.
  • Combine git technique.
  • Extend to Python and LaTex projects.
  • Use graphviz to draw graphics in Hackmd and LaTex.

Main Text







foo



a

xyz.c & xyz.h



b

xyz.o



a:b->b:data



next



c

a.out



b:c->c



prev



d

.....



b:c->d





c:c->a



prev



c:c->b:data



next



  • In modern softwares, a program is divided into several modules. reasons?
  • Each module is compiled separately (as object files, (say those .o files in Linux) and they are linked to generate the executable file (say those .exe files in Windows).
  • Make is a program which automates building dependency trees.
  • Makefiles are a list of with Make rules which include target, dependency (source), and recipe (command).
    • Make builds based on structural organization of how code depends on other code as defined by includes.
    • If a source is also a target for other sources, must also evaluate its dependencies and remake as required.
    • Make can check when you've last edited each file, and only build what is needed!
      • Files have "last modification date". make can check whether the sources are more recent than the target.
    • Make isn’t language specific: recipe can be any valid shell command. That is why I will extend to my own projects for automation purpose!
  • The basic usage of make is as follows.
    ​​​​ make [-f some_makefile] [ other_options ] [ targets ]
    • Use -f for a specific makefile name. If not, make goes with the default name Makefile.
    • If no target, make will default to the first item listed in the makefile.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • When a sequence of Unix commands is placed inside parentheses (), a new subprocess is created, and the commands are executed as part of that subprocess.
    • When the subprocess terminates, the parent process resumes in the original directory. No additional cd command is needed.

Case Studies

Case 1 No Makefile

Case 2 Simple Makefile

Case 3 A Little Bit Complex Makefile

Case 4 A Big Deal Makefile

References

Official Documents

Lecture Slides

Books

  • John Graham-Cumming, The GNU Make Book, 2015
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →