How to create makefile in Linux?

Creating a Makefile in Linux: A Step-by-Step Guide

Introduction

A Makefile is a crucial tool in Linux that allows you to automate the compilation, installation, and packaging of software projects. It’s a text file that contains a set of rules and commands that define how to build and install a project. In this article, we’ll show you how to create a Makefile from scratch, including the basic syntax, common features, and best practices.

Basic Syntax

A Makefile consists of a series of rules and commands that are defined using a specific syntax. Here’s a breakdown of the basic syntax:

  • Variables: You can define variables using the VARIABLE_NAME syntax. For example: VARIABLE_NAME = value
  • Rules: A rule is defined using the rule syntax. For example: rule: dependency1 dependency2
  • Dependencies: Dependencies are listed after the rule keyword. For example: rule: dependency1 dependency2
  • Commands: Commands are executed using the command syntax. For example: command: dependency1 dependency2

Common Features

Here are some common features of Makefiles:

  • Conditional Statements: You can use conditional statements to execute different commands based on certain conditions. For example: rule: dependency1 dependency2 if condition
  • Lists: You can use lists to specify multiple dependencies or commands. For example: rule: dependency1 dependency2 dependency3
  • Variables: You can use variables to store values that can be used in multiple rules. For example: VARIABLE_NAME = value
  • Conditional Statements: You can use conditional statements to execute different commands based on certain conditions. For example: rule: dependency1 dependency2 if condition

Best Practices

Here are some best practices to keep in mind when creating a Makefile:

  • Keep it Simple: Keep your Makefile simple and focused on the specific task at hand.
  • Use Variables: Use variables to store values that can be used in multiple rules.
  • Use Conditional Statements: Use conditional statements to execute different commands based on certain conditions.
  • Test Your Makefile: Test your Makefile thoroughly to ensure it works as expected.

Creating a Makefile from Scratch

Here’s an example of a simple Makefile that compiles a C program:

# Define the variables
C_SOURCE = C
CFLAGS = -Wall -O2
LDFLAGS = -static

# Define the rules
all: hello.c
hello.o: hello.c
hello.o: C_SOURCE
hello.o: CFLAGS
hello.o: LDFLAGS

hello.o: hello.c
$(CFLAGS) -c hello.c -o hello.o

hello: hello.o
$(LDFLAGS) -static hello.o -o hello

# Define the dependencies
hello.o: C_SOURCE
hello.o: CFLAGS
hello.o: LDFLAGS

# Define the commands
hello: $(hello.o)
$(LDFLAGS) -static $(hello.o) -o hello

hello.o: $(C_SOURCE)
$(CFLAGS) -c $(C_SOURCE) -o $(C_SOURCE).o

# Define the dependencies
C_SOURCE: C
C_SOURCE: CFLAGS
C_SOURCE: LDFLAGS

Explanation

This Makefile defines the following rules:

  • all: hello.c: Compiles the hello.c file.
  • hello.o: hello.c: Compiles the hello.c file.
  • hello.o: C_SOURCE: Compiles the hello.c file using the C_SOURCE variable.
  • hello.o: CFLAGS: Compiles the hello.c file using the CFLAGS variable.
  • hello.o: LDFLAGS: Compiles the hello.c file using the LDFLAGS variable.
  • hello: hello.o: Compiles the hello executable using the hello.o object file.
  • hello.o: $(C_SOURCE): Compiles the hello.c file using the C_SOURCE variable.
  • hello.o: $(CFLAGS): Compiles the hello.c file using the CFLAGS variable.
  • hello.o: $(LDFLAGS): Compiles the hello.c file using the LDFLAGS variable.

Using Makefiles with Multiple Projects

Here’s an example of a Makefile that compiles and installs a C program for multiple projects:

# Define the variables
PROJECT1_C_SOURCE = project1.c
PROJECT1_CFLAGS = -Wall -O2
PROJECT1_LDFLAGS = -static

PROJECT2_C_SOURCE = project2.c
PROJECT2_CFLAGS = -Wall -O2
PROJECT2_LDFLAGS = -static

# Define the rules
all: hello1 hello2
hello1: $(PROJECT1_C_SOURCE)
hello1: $(PROJECT1_CFLAGS)
hello1: $(PROJECT1_LDFLAGS)

hello2: $(PROJECT2_C_SOURCE)
hello2: $(PROJECT2_CFLAGS)
hello2: $(PROJECT2_LDFLAGS)

hello1: $(PROJECT1_C_SOURCE)
$(PROJECT1_CFLAGS) -c $(PROJECT1_C_SOURCE) -o $(PROJECT1_C_SOURCE).o
$(PROJECT1_LDFLAGS) -static $(PROJECT1_C_SOURCE).o -o $(PROJECT1_C_SOURCE).o
hello2: $(PROJECT2_C_SOURCE)
$(PROJECT2_CFLAGS) -c $(PROJECT2_C_SOURCE) -o $(PROJECT2_C_SOURCE).o
$(PROJECT2_LDFLAGS) -static $(PROJECT2_C_SOURCE).o -o $(PROJECT2_C_SOURCE).o

# Define the dependencies
hello1: $(PROJECT1_C_SOURCE)
hello1: $(PROJECT1_CFLAGS)
hello1: $(PROJECT1_LDFLAGS)

hello2: $(PROJECT2_C_SOURCE)
hello2: $(PROJECT2_CFLAGS)
hello2: $(PROJECT2_LDFLAGS)

# Define the commands
hello1: $(PROJECT1_C_SOURCE)
$(PROJECT1_CFLAGS) -c $(PROJECT1_C_SOURCE) -o $(PROJECT1_C_SOURCE).o
$(PROJECT1_LDFLAGS) -static $(PROJECT1_C_SOURCE).o -o $(PROJECT1_C_SOURCE).o
hello2: $(PROJECT2_C_SOURCE)
$(PROJECT2_CFLAGS) -c $(PROJECT2_C_SOURCE) -o $(PROJECT2_C_SOURCE).o
$(PROJECT2_LDFLAGS) -static $(PROJECT2_C_SOURCE).o -o $(PROJECT2_C_SOURCE).o

# Define the dependencies
PROJECT1_C_SOURCE: C_SOURCE
PROJECT1_C_SOURCE: CFLAGS
PROJECT1_C_SOURCE: LDFLAGS

PROJECT2_C_SOURCE: C_SOURCE
PROJECT2_C_SOURCE: CFLAGS
PROJECT2_C_SOURCE: LDFLAGS

Explanation

This Makefile defines the following rules:

  • all: hello1 hello2: Compiles and installs the hello1 and hello2 projects.
  • hello1: $(PROJECT1_C_SOURCE): Compiles the hello1 project.
  • hello1: $(PROJECT1_CFLAGS): Compiles the hello1 project.
  • hello1: $(PROJECT1_LDFLAGS): Compiles the hello1 project.
  • hello2: $(PROJECT2_C_SOURCE): Compiles the hello2 project.
  • hello2: $(PROJECT2_CFLAGS): Compiles the hello2 project.
  • hello2: $(PROJECT2_LDFLAGS): Compiles the hello2 project.
  • hello1: $(PROJECT1_C_SOURCE): Compiles the hello1 project.
  • hello2: $(PROJECT2_C_SOURCE): Compiles the hello2 project.

Best Practices for Makefiles

Here are some best practices for Makefiles:

  • Keep it Simple: Keep your Makefile simple and focused on the specific task at hand.
  • Use Variables: Use variables to store values that can be used in multiple rules.
  • Use Conditional Statements: Use conditional statements to execute different commands based on certain conditions.
  • Test Your Makefile: Test your Makefile thoroughly to ensure it works as expected.
  • Use Makefiles with Multiple Projects: Use Makefiles to manage multiple projects and dependencies.

Conclusion

Creating a Makefile in Linux is a straightforward process that requires understanding the basic syntax and common features. By following the steps outlined in this article, you can create a Makefile that automates the compilation, installation, and packaging of your software projects. Remember to keep your Makefile simple, use variables, and test it thoroughly to ensure it works as expected.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top