Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Makefiles in Linux so useful?

Tags:

c

linux

makefile

I want to know why makefiles in Linux are so useful (I mean in the practical sense). Why can't we just compile all our programs in the normal way?

like image 701
station Avatar asked Jul 24 '11 05:07

station


People also ask

Why do we use makefiles?

Makefile sets a set of rules to determine which parts of a program need to be recompile, and issues command to recompile them. Makefile is a way of automating software building procedure and other complex tasks with dependencies. Makefile contains: dependency rules, macros and suffix(or implicit) rules.

Why makefile is used in Linux?

Makefile is a program building tool which runs on Unix, Linux, and their flavors. It aids in simplifying building program executables that may need various modules. To determine how the modules need to be compiled or recompiled together, make takes the help of user-defined makefiles.

Are makefiles still relevant?

Makefiles are not obsolete, in the same way that text files are not obsolete. Storing all data in plain text is not always the right way of doing things, but if all you want is a Todo List then a plain text file is fine.

What advantages to the build process does a makefile provide?

Advantages Of Makefiles Makefiles automatically compile only those files that are changed. Thus we need not regenerate the entire project when some of the portions of the project are modified. Make tool allows us to compile multiple files at once so that all the files can be compiled in a single step.


2 Answers

The normal way for Linux is to use a make file.

It evolved out of all the mistakes people made by compiling ever more complex applications by hand or with home-made scripts. It is non-trivial to represent build dependencies for a complex project. Make simply offers a standardized way to specify such dependencies and a tool to parse the dependencies and run associated build actions.

Even UI's that simplify/automate the build process for you use a make file or something similar behind the scenes.

UPDATE

In for those wondering about the automake comment, here are two differing views on the topic

http://www.freesoftwaremagazine.com/books/autotools_a_guide_to_autoconf_automake_libtool

http://www.scurrilous.com/blog/archives/2005/08/23/i-hate-automake/

like image 199
Eric J. Avatar answered Sep 19 '22 14:09

Eric J.


A makefile is useful because (if properly defined) allows recompiling only what is needed when you make a change.

In a large project rebuilding the program can take some serious time because there will be many files to be compiled and linked and there will be documentation, tests, examples etc. When you work on the project and you make a little change it would be annoying having to wait to rebuild everything each time.

A makefile store a list "input" files, "output" files and "commands" needed to produce the output given the input. When you make a change to the project the command make will check the date of the input files with the date of the corresponding output files and if the input files has been changed it will recreate the corresponding output by running the command.

This is of course just a very rough description as make is much more sophisticated than this (topological sort, macro commands, parallel execution)... but it should give you an idea.

Rebuilding everything at each change would take too much time, and rebuilding manually only what is needed is error prone (if you forget to rebuild a piece you may end up thinking that a change you made is ok, while indeed it is breaking something that you simply did not recompile).

Note that today there is a plethora of build systems much more sophisticated than makefiles (that for example generate makefiles automatically by analyzing your platform and source code).

That those tools are a silver bullet or a solution looking for a problem is questionable (I'm biased on this because I hate build tools and they hate me... somehow their autodetect logic never works correctly on my machines).

like image 27
6502 Avatar answered Sep 22 '22 14:09

6502