What is the difference between makefile and sh file. sh(shell script file) file also can work at the place of makefile(means we can do same thing using shell file like makefile can do) then what is difference between them. there is any execution difference or any standard where we have to use makefile and sh file.
one Example of this to compile a hello.c file with Makefile and shell file
shell.sh
param="$1";
CC="gcc"
CFLAGS="-c -Wall";
if [ "$param" == "clean" ];
then
rm -rf hello
else
$CC $CFLAGS hello.c -o hello
fi
./shell.sh
{ will build hello.c file }./shell.sh clean
{ this will clean the build file }Same thing with Makefile..
CC=gcc
CFLAGS=-c -Wall
hello: hello.c
$(CC) $(CFLAGS) hello.c -o hello
clean:
rm -rf hello
make
{it will build}make clean
{it will clean build file}
Both files can generate same output. This question because some people use Makefile and some people use shell..
A Makefile is an example of Declarative programming . Shell scripts are imperative. To illustrate the difference, try to write a shell script that does what a Makefile does: run build steps according to dependency rules, executing only the needed steps.
A shell script or sh-file is something between a single command and a (not necessarily) small programm. The basic idea is to chain a few shell commands together in a file for ease of use. So whenever you tell the shell to execute that file, it will execute all the specified commands in order.
A makefile is a script. It is interpreted by some version of Make. The language of makefiles is designed to be used in build systems, invoking shell commands to build files out of other files.
So put SHELL := /bin/bash at the top of your makefile, and you should be good to go. See "Target-specific Variable Values" in the documentation for more details. That line can go anywhere in the Makefile, it doesn't have to be immediately before the target.
make
automatically checks (based on time stamps) which targets need to be remade and which ones can be left untouched. If you write your own shell script, you'll either have to program this logic yourself or else all your components will be rebuilt when you run your script - even those that haven't changed since the last build.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With