目录结构:
Include—|
|—mystr.h
|—mylib.h
User——|
|—main.c
|—mystr.c
|—mylib.c
Makefile
生成可以执行文件:test
运行环境:macOS shell
Makefile:
#$^: 所有不重复的依赖文件
#$@: 目标文件的完整名称
#$<: 第一个依赖文件的名称
#预处理: gcc -E -o file.i file.c
#编译: gcc -S -o file.s file.i
#汇编: gcc -c file.s -o file.o
#连接: gcc file.o -o file
OBJS = main.o mystr.o mylib.o
CC = gcc
CFLAGS = -Wall -g
vpath %.c User
vpath %.h Include
test: $(OBJS)
$(CC) $^ -o $@
mystr.o: mystr.c mystr.h
$(CC) -I ./Include $(CFLAGS) -c $< -o $@
mylib.o: mylib.c mylib.h
$(CC) -I ./Include $(CFLAGS) -c $< -o $@
main.o: main.c mystr.c mylib.c
$(CC) -I ./Include $(CFLAGS) -c $< -o $@
.PHONY : clean
clean :
@echo "Deleting files ..."
rm -rf test $(OBJS)
运行结果: