
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Makefile for add, sub, mul, and div programs in C
What is a Makefile?
A makefile is a unique file used in many programming projects to produce executable programs automatically by compiling and linking them. It provides instructions on linking and compiling a program, along with a list of source file dependencies and building commands. The program called make is what reads a Makefile and runs the required commands.
General format of Makefile
- Specify the compiler (gcc for C programming and g++ for C++ programming).
- Specify the compiler flags.
- Define target executables.
- Define default target (all).
- Compile and link source files to executables.(can also be done using object files).
- Clean target to remove executable.
Lets see example of makefile for addition, subtraction, multiplication, and division programs in C.
Makefile for add, sub, mul, and div Programs
# Compiler to use
CC = gcc
# Compiler flags
CFLAGS = -Wall -g
# Target executable names
TARGETS = add sub mul div
# Default target when running "make"
all: $(TARGETS)
run_add:add
@./add
run_sub:sub
@./sub
run_mul:mul
@./mul
run_div:div
@./div
# Rule for creating the addition program
add: add.c
@$(CC) $(CFLAGS) -o add add.c
# Rule for creating the subtraction program
sub: sub.c
@$(CC) $(CFLAGS) -o sub sub.c
# Rule for creating the multiplication program
mul: mul.c
@$(CC) $(CFLAGS) -o mul mul.c
# Rule for creating the division program
div: div.c
@$(CC) $(CFLAGS) -o div div.c
# Clean up object files and executables
clean:
rm -f $(TARGETS)
Makefile Execution Explanation
- CC = gcc: This line sets gcc as the compiler to be used for compiling the C programs.
- CFLAGS = -Wall -g: Here, -Wall enables all warnings which helps to identify potential issues in the code. -g generates debugging information with tools like gdb.
- Here, TARGETS is a variable which contains names of executables. later which we can refer to all targets with $(TARGETS) rather than typing each name repeatedly.
- all: This is the default target that runs when make is called without any specific instruction.
-
add: add.c: It defines the rule to compile add.c into an executable add
@$(CC) $(CFLAGS) -o add add.c runs the compiler command with gcc and specified flags (-Wall -g) to create the add executable. Similarly for other operations. - run_<operation> allows to compile and run each program run_add compiles add if it is not already built. ./add runs the add executable. @ is used before that to suppress the output. Similarly for other programs.
- clean target is used to remove the compiled executables.
Here are the programs for addition, subtraction, multiplication and division programs in C.
Program Files
add.c
#include <stdio.h> int main() { int a, b; printf("Enter two integers to add: "); scanf("%d %d", &a, &b); printf("Sum: %d
", a + b); return 0; }
sub.c
#include <stdio.h> int main() { int a, b; printf("Enter two integers to subtract: "); scanf("%d %d", &a, &b); printf("Difference: %d
", a - b); return 0; }
mul.c
#include <stdio.h> int main() { int a, b; printf("Enter two integers to multiply: "); scanf("%d %d", &a, &b); printf("Product: %d
", a * b); return 0; }
div.c
#include <stdio.h> int main() { int a, b; printf("Enter two integers to divide: "); scanf("%d %d", &a, &b); if (b != 0) printf("Quotient: %d
", a / b); else printf("Error: Division by zero.
"); return 0; }
Here Makefile and its related programs are under one directory.
run make for specific operations.
Advertisements