1 / 15

The Makefile utility

The Makefile utility. COP 3402 (Fall 2014). Motivation. Small programs single file. “ Not so small ” programs: Many lines of code. Multiple components. More than one programmer. Motivation – continued. Problems : Long files are harder to manage

ethan-floyd
Download Presentation

The Makefile utility

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Makefile utility COP 3402 (Fall 2014)

  2. Motivation • Small programs single file. • “Not so small”programs: • Many lines of code. • Multiple components. • More than one programmer.

  3. Motivation – continued • Problems: • Long files are harder to manage (for both programmers and machines)‏. • Every change requires long compilation. • Many programmers can not modify the same file simultaneously. • Division to components is desired.

  4. Motivation – continued • Solution : divide project to multiple files. • Targets: • Good division to components. • Minimum compilation when something is changed. • Easy maintenance of project structure, dependencies and creation.

  5. Project maintenance • Done in Unix by the Makefilemechanism. • A makefile is a file (script) containing: • Project structure (files, dependencies).‏ • Instructions for files creation. • The make command reads a makefile, understands the project structure and creates the executable. • Note that the Makefile mechanism is not limited to C programs.

  6. Project structure • Project structure and dependencies can be represented as a DAG(Directed Acyclic Graph). • Example: • Program contains 3 files: • main.c., sum.c, sum.h • sum.h included in both .c files. • Executable should be the file sum.

  7. sum (exe)‏ sum.o main.o main.c sum.h sum.c sum.h Project structure

  8. makefile sum: main.osum.o gcc –o sum main.osum.o main.o: main.csum.h gcc –c main.c sum.o: sum.csum.h gcc –c sum.c

  9. Rule syntax main.o: main.csum.h gcc –c main.c Rule target action dependency

  10. Equivalent makefiles • .o depends (by default) on corresponding .c file. Therefore, an equivalent makefile is: sum: main.osum.o gcc –o sum main.osum.o main.o: sum.h gcc –c main.c sum.o: sum.h gcc –c sum.c

  11. make operation • Project dependencies tree is constructed. • Target of first rule should be created. • We go down the tree to see if there is a target that should be recreated. This is the case when the target file is older than one of its dependencies. • In this case we recreate the target file according to the action specified, on our way up the tree. Consequently, more files may need to be recreated. • If something is changed, linking is usually necessary.

  12. make operation - continued • make operation ensures minimum compilation, when the project structure is written properly. • Do not writesomething like: prog: main.c sum1.c sum2.c gcc –o progmain.c sum1.c sum2.c which requires compilation of the full projectwhen something is changed.

  13. Make operation - example FileLast Modified sum 10:03 main.o 09:56 sum.o09:35 main.c10:45 sum.c09:14 sum.h08:39

  14. Make operation - example • Operations performed: gcc –c main.c gcc –o sum main.osum.o • main.o should be recompiled (main.c is newer). • Consequently, main.o is newer than sum and therefore sum should be recreated (by re-linking).

  15. Reference • Good tutorial for makefiles: http://www.gnu.org/software/make/manual/make.html

More Related