06mkfile-03tree

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

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

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

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

sinclude $(DEP)

.PHONY: clean

clean:
    $(RM) $(SRCS:.c=.o) $(BUILT_IN) $(DEP)
~                                            


main/main.c

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

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

    return 0;
}


main/Makefile

SRCS := main.c

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

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

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

sinclude $(DEP)

.PHONY: clean

clean:
    $(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)
~                                   


 

你可能感兴趣的:(06mkfile-03tree)