Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between makefile and sh file

Tags:

c

linux

shell

sh

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
  1. ./shell.sh { will build hello.c file }
  2. ./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..

like image 726
Vinod Patidar Avatar asked Mar 22 '14 14:03

Vinod Patidar


People also ask

What is the difference between makefile and shell script?

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.

What is .sh file used for?

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.

Is a makefile a script?

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.

Can I use bash in makefile?

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.


1 Answers

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.

like image 169
sepp2k Avatar answered Oct 18 '22 16:10

sepp2k