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.
$ tree . ├── inc │ ├── add.h │ └── sub.h ├── Makefile └── src ├── add.c ├── main.c └── sub.c
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/
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.
$ 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

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
Recommended Reading
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“!
Reply “Join Group” in the backend to join the technical exchange group. Benefits of joining:Free Linux learning materials.