Make seems to me simply a shell script with slightly easier handling of command line arguments.
Why is it standard to run make instead of ./make.sh
Make seems to me simply a shell script with slightly easier handling of command line arguments.
Why is it standard to run make instead of ./make.sh
Makefiles are usually automatically generated by your development toolkit, through ./configure scripts. This makes it easier for the developer, because they don't have to make their own build script.
The general idea is that make
supports (reasonably) minimal rebuilds -- i.e., you tell it what parts of your program depend on what other parts. When you update some part of the program, it only rebuilds the parts that depend on that. While you could do this with a shell script, it would be a lot more work (explicitly checking the last-modified dates on all the files, etc.) The only obvious alternative with a shell script is to rebuild everything every time. For tiny projects this is a perfectly reasonable approach, but for a big project a complete rebuild could easily take an hour or more -- using make
, you might easily accomplish the same thing in a minute or two...
make handles dependencies: the makefile describes them: the binary depends on object files, each object file depends on a source file and headers ... when make is ran, the date of the files are compared to determine what needs to be re-compiled.
One can invoke directly one target not to build everything described in the Makefile.
Moreover the make syntax provides substitution, vpath
All of this can be written in shell scripts, with make you already have it.
Make ensures that only the required files are recompiled when you make changes to your source files.
For example:
final : 1.o 2.o
gcc -o final 1.o 2.o
1.o : 1.c 2.h
gcc -c 1.c
2.o : 2.c 2.h
gcc -c 2.c
If I change the file 2.h
only & run make
, it executes all the 3 commands, in reverse order.
If I change the file 1.c
only & run make
, it only executes the first 2 commands in reverse order.
Trying to accomplish that with your own shell script will involve a lot of if/else
checking.
There are various things make does that are hard to do with shell scripts...
As well as the above, Make is a declarative(-ish) parallel programming language.
Let's say that you have 4,000 graphic files to convert and 4 CPUs. Try writing a 10-line shell script (I'm being generous here) that will do it reliably while saturating your CPUs.
Perhaps the real question is why do people bother writing shell scripts.