What Are Makefile in c? A Makefile in c is a special file used by the make build automation tool. It defines a set of rules for compiling and linking programs. Makefiles in c help automate the process of compiling complex programs by specifying how to derive the target program from source files. Structure of a Makefile in c A Makefile consists of three main parts: Variables Rules Targets 1. Variables Variables in a Makefile store paths, compiler options, and other parameters that can be used throughout the Makefile. CC = gcc CFLAGS = -Wall -g 2. Rules Rules specify how to build targets from dependencies. A rule looks like this: target: dependencies command to build target 3. Targets Targets are the files or programs you want to generate, such as executables or object files. Basic Makefile Example Here’s a basic Makefile for compiling a C program: CC = gcc CFLAGS = -Wall -g # Target to build executable my_program: main.o math.o $(CC) $(CFLAGS) -o my_program main.o math.o # Target to build object file from source main.o: main.c $(CC) $(CFLAGS) -c main.c math.o: math.c $(CC) $(CFLAGS) -c math.c How it Works: The target my_program depends on main.o and math.o. The rule specifies how to create these object files and link them into an executable. Advanced Makefile Concepts 1. Automatic Variables Makefiles support automatic variables, such as $@ (target), $< (first dependency), and $^ (all dependencies). Example: %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ 2. Pattern Rules Pattern rules allow you to define general rules that apply to multiple files. %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ 3. Phony Targets Phony targets are not actual files. They are used for actions like clean or install. makefileCopy code.PHONY: cleanclean: rm -f *.o my_program Mini Project: Makefile in C Program Let’s create a makefile in c for a program that consists of multiple C files. Create the C files (main.c, math.c). Create the Makefile: CC = gcc CFLAGS = -Wall -g my_program: main.o math.o $(CC) $(CFLAGS) -o my_program main.o math.o main.o: main.c $(CC) $(CFLAGS) -c main.c math.o: math.c $(CC) $(CFLAGS) -c math.c .PHONY: clean clean: rm -f *.o my_program Run make to compile the program and make clean to remove object files and the executable. Interview Questions and Answers GoogleQ1: What is the purpose of a Makefile?A: A Makefile automates the process of compiling and linking programs, specifying how to build the program from source files. TCSQ2: What is the use of variables in a Makefile?A: Variables store paths, compiler options, and other parameters, making it easier to manage and modify the build process. InfosysQ3: How do automatic variables work in Makefiles?A: Automatic variables like $@, $<, and $^ represent the target file, first dependency, and all dependencies, respectively. ZohoQ4: What are phony targets in a Makefile?A: Phony targets are not actual files but are used for actions like clean, ensuring that these targets are always executed when called. AmazonQ5: How does a pattern rule work in a Makefile?A: Pattern rules allow you to define general rules for building multiple files with similar naming conventions, reducing redundancy.