Practical Examples of Makefile

Clickthe blue text

Follow us

We previously described the concepts of Makefile; now we provide an example to see how to use it, and thus conclude the topic of Makefile.

The directory and file contents we prepared are as follows:
$ tree  .  ├── inc  │   ├── add.h  │   └── sub.h  ├── Makefile  └── src      ├── add.c      ├── main.c      └── sub.c
The content of the Makefile is as follows:
VERSION = 1.0.0SOURCE = $(wildcard ./src/*.c)OBJECT = $(patsubst %.c, %.o, $(SOURCE))
INCLUEDS = -I ./inc
TARGET  = riceCC      = gccCFLAGS  = -Wall -g
$(TARGET): $(OBJECT)  @mkdir -p output/  $(CC) $^ $(CFLAGES) -o output/$(TARGET)_$(VERSION)
%.o: %.c  $(CC) $(INCLUEDS) $(CFLAGES) -c $< -o $@
.PHONY:clean
clean:  @rm -rf $(OBJECT) output/
Analysis:

Line 1: Assign the version number to the variable VERSION

Line 2: Get all .c files in the current directory src and assign them to the variable SOURCE.

Line 3: Replace the .c files in the ./src directory with .o files and assign them to OBJECT.

Line 4: Specify the header file directory using the -I option and assign it to the variable INCLUDES.

Line 7: The name of the final target file is rice, assigned to TARGET.

Line 8: Replace the default CC from cc to gcc.

Line 9: Assign the option to display all warning messages and gdb debugging options to the variable CFLAGS.

Line 12: Create the directory output and suppress the output of this command in the terminal.

Line 13: The executable program 100ask will be generated in the output directory, with the version number as a suffix.

Line 16: Generate the corresponding object files from the source files.

Line 18: A phony target to avoid naming conflicts with a file named clean in the current directory.

Line 20: The command executed when running make clean is to delete the files generated during the compilation process.

The final compilation result is as follows:
$ make  gcc -I ./inc  -c src/main.c -o src/main.o  gcc -I ./inc  -c src/add.c -o src/add.o  gcc -I ./inc  -c src/sub.c -o src/sub.o  gcc src/main.o src/add.o src/sub.o  -o output/rice$tree  .  ├── inc  │   ├── add.h  │   └── sub.h  ├── Makefile  ├── output  │   └── rice_1.0.0  └── src      ├── add.c      ├── add.o      ├── main.c      ├── main.o      ├── sub.c      └── sub.o
Practical Examples of Makefile

Personal WeChat of Yikoujun

Add Yikoujun’s personal WeChat to receive exclusive introductory videos on Linux, embedded systems, etc.

→ Selected technical materials shared

→ A community for交流 with experts

Practical Examples of Makefile

Recommended Reading

【1】Detailed explanation of ARM UART bare-metal driver 【2】Linux spinlock spinlock, teaching you how to deadlock ubuntu 【3】After reading these C language examples, you will surely exclaim five times just like me, each time louder than the last[Must Read]【4】Step-by-step guide to Linux driver 7 – kernel mutex 【5】Linux semaphore (1) – SYSTEM V【6】I2C essentials – based on Cortex-A9 (reorganized) [Must Read]

【7】Understanding ADC bare-metal and Linux driver writing methods in one article【8】22 images explaining how browser requests reach web servers (understand networking to graduate) 【9】Implement a Linux socket chat room from scratch – multi-threaded server model – 1 [Must Read]【10】Implement a Linux socket chat room from scratch – multi-threaded server with a subtle error – 2[Must Read]

All original content from this public account has been organized into a directory. Please reply with “m” in the public account to get it! Or follow and click on the bottom left corner “Content“!

Practical Examples of Makefile

Reply “Join Group” in the backend to join the technical exchange group. Benefits of joining:Free Linux learning materials.

Leave a Comment