C实例---Makefile工程管理

目录结构:
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)

运行结果:
这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/huazhen1234/article/details/55050569
今日推荐