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)