07mkfile

include/func_a.h

void func_a();

 

include/func_b.h

void func_b();

 

lib/func_a.c

#include <stdio.h>
#include "func_a.h"

void func_a()
{
    printf("%s\n", __FUNCTION__);
}


lib/func_b.c

 

#include <stdio.h>
#include "func_b.h"

void func_b()
{
    printf("%s\n", __FUNCTION__);
}


lib/Makefile

SRCS := func_a.c func_b.c
SUBDIR := usr 

$(BUILT_IN): $(SRCS:.c=.o) $(SUBDIR)
    $(LD) $(LDFLAGS) -r -o $@ $(SRCS:.c=.o) $(SUBDIR:=/$(BUILT_IN))

$(SUBDIR):
    $(MAKE) -C $@

$(SRCS:.c=.o):
    $(CC) $(CFLAGS) -c $<

$(DEP): $(SRCS)
    $(CC) $(CFLAGS) -MM $(SRCS) >$@
    sed -i "s/: / $@: /g" $@

sinclude $(DEP)

.PHONY: clean $(SUBDIR)

clean:
    for dir in $(SUBDIR); do \
        $(MAKE) -C $$dir clean; \
    done
    $(RM) $(SRCS:.c=.o) $(BUILT_IN) $(DEP)


lib/usr/Makefile

SRCS := usr.c
SUBDIR := 

$(BUILT_IN): $(SRCS:.c=.o) $(SUBDIR)
    $(LD) $(LDFLAGS) -r -o $@ $(SRCS:.c=.o) $(SUBDIR:=/$(BUILT_IN))

$(SUBDIR):
    $(MAKE) -C $@

$(SRCS:.c=.o):
    $(CC) $(CFLAGS) -c $<

$(DEP): $(SRCS)
    $(CC) $(CFLAGS) -MM $(SRCS) >$@
    sed -i "s/: / $@: /g" $@

sinclude $(DEP)

.PHONY: clean $(SUBDIR)

clean:
    for dir in $(SUBDIR); do \
        $(MAKE) -C $$dir clean; \
    done
    $(RM) $(SRCS:.c=.o) $(BUILT_IN) $(DEP)
~                                              


lib/usr/usr.c

#include <stdio.h>
#include "usr.h"

void usr()
{
    printf("%s\n", __FUNCTION__);
}
~                                   


main/main.c

#include <stdio.h>
#include "func_a.h"
#include "func_b.h"
#include "usr.h"

int main()
{
    func_a();
    func_b();
    usr();

    return 0;
}
~                  


main/Makefile

SRCS := main.c
SUBDIR := 

$(BUILT_IN): $(SRCS:.c=.o) $(SUBDIR)
    $(LD) $(LDFLAGS) -r -o $@ $(SRCS:.c=.o) $(SUBDIR:=/$(BUILT_IN))

$(SUBDIR):
    $(MAKE) -C $@

$(SRCS:.c=.o):
    $(CC) $(CFLAGS) -c $<

$(DEP): $(SRCS)
    $(CC) $(CFLAGS) -MM $(SRCS) >$@
    sed -i "s/: / $@: /g" $@

sinclude $(DEP)

.PHONY: clean $(SUBDIR)

clean:
    for dir in $(SUBDIR); do \
        $(MAKE) -C $$dir clean; \
    done
    $(RM) $(SRCS:.c=.o) $(BUILT_IN) $(DEP)


 

Makefile

TGT := hello
SUBDIR := \
      lib \
      main

export TOPDIR := $(shell pwd)
export CFLAGS := -O2 -I$(TOPDIR)/include
export LDFLAGS :=  

export CROSS_COMPILER_PREFIX :=  
export CC := $(CROSS_COMPILER_PREFIX)gcc
export LD := $(CROSS_COMPILER_PREFIX)ld

export BUILT_IN := built_in.o
export DEP = dep 
export MAKE := make

all: $(TGT)

$(TGT): $(SUBDIR)
    $(CC) $(LDFLAGS) -o $@ $(^:=/$(BUILT_IN))

$(SUBDIR):
    $(MAKE) -C $@

.PHONY: $(SUBDIR) clean all 

clean:
    for dir in $(SUBDIR); do \
        $(MAKE) -C $$dir clean; \
    done
    $(RM) $(TGT)


                                             

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值