10-变量与函数的综合示例

1、实战需求

这里写图片描述

2、工具原料

这里写图片描述

3、关键技巧

这里写图片描述

4、规则中的模式替换

这里写图片描述

5、编译规则依赖

这里写图片描述

6、小结

这里写图片描述

7、示例

CC := gcc
MKDIR := mkdir
RM := rm -fr

DIR_OBJS := objs
DIR_TARGET := target

DIRS := $(DIR_OBJS) $(DIR_TARGET)

TARGET := $(DIR_TARGET)/hello-makefile.out
# main.c const.c func.c
SRCS := $(wildcard *.c)
# main.o const.o func.o
OBJS := $(SRCS:.c=.o)
# objs/main.o objs/const.o objs/func.o
OBJS := $(addprefix $(DIR_OBJS)/, $(OBJS))

.PHONY : rebuild clean all

$(TARGET) : $(DIRS) $(OBJS)
    $(CC) -o $@ $(OBJS)
    @echo "Target File ==> $@"

$(DIRS) :
    $(MKDIR) $@


$(DIR_OBJS)/%.o : %.c
    ifeq ($(DEBUG),true)
        $(CC) -o $@ -g -c $^ 
    else   
        $(CC) -o $@ -c $^
    endif

rebuild : clean all

all : $(TARGET)

clean :
    $(RM) $(DIRS)

输出:make
mkdir objs
mkdir target
gcc -o objs/const.o -c const.c
gcc -o objs/main.o -c main.c
gcc -o objs/func.o -c func.c
gcc -o target/hello-makefile.out objs/const.o objs/main.o objs/func.o
Target File ==> target/hello-makefile.out


编译debug调试版本
输出:make DEBUG:=ture
mkdir objs
mkdir target
gcc -o objs/const.o -c const.c
gcc -o objs/main.o -c main.c
gcc -o objs/func.o -c func.c
gcc -o target/hello-makefile.out objs/const.o objs/main.o objs/func.o
Target File ==> target/hello-makefile.out

进入cd target 目录
使用objdump -S hello-makefile.out
查看可执行文件的调试信息

最终目录结构:
---./
   -main.c
   -func.c
   -const.c
   -makefile
   -objs
       -mian.o
       -func.o
       -const.o
   -target
       -hello-makefile.out

这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值