Skip to content

Instantly share code, notes, and snippets.

@gnosis23
Last active August 29, 2015 14:11
Show Gist options
  • Save gnosis23/c0576115393644048b15 to your computer and use it in GitHub Desktop.
Save gnosis23/c0576115393644048b15 to your computer and use it in GitHub Desktop.
makefile examples
CFLAGS = -g -Wall -D_DEBUG_ -D_GNU_SOURCE $(ARCH) -Ilib/ -Isrc/
LIBS= $(SOCK) -lm -lpthread
PFLAGS= -follow-child-processes=yes -cache-dir=/tmp/${USER}
PURIFY= purify ${PFLAGS}
# Add any header files you've added here
sr_HDRS = lib/sha1.h lib/sr_dumper.h lib/sr_if.h lib/sr_rt.h lib/sr_utils.h \
lib/vnscommand.h src/sr_arpcache.h src/sr_protocol.h src/sr_router.h
# Add any source files you've added here
sr_SRCS = lib/sha1.c lib/sr_dumper.c lib/sr_if.c lib/sr_rt.c lib/sr_utils.c \
lib/sr_vns_comm.c src/sr_arpcache.c src/sr_main.c src/sr_router.c
sr_OBJS = $(patsubst %.c,%.o,$(sr_SRCS))
sr_DEPS = $(patsubst %.c,%.d,$(sr_SRCS))
hurr :
@echo $(sr_DEPS)
$(sr_OBJS) : %.o : %.c
$(CC) -c $(CFLAGS) $< -o $@
$(sr_DEPS) : %.d : %.c
$(CC) -MM $(CFLAGS) $< > $@
include $(sr_DEPS)
sr : $(sr_OBJS)
$(CC) $(CFLAGS) -o sr $(sr_OBJS) $(LIBS)
sr.purify : $(sr_OBJS)
$(PURIFY) $(CC) $(CFLAGS) -o sr.purify $(sr_OBJS) $(LIBS)
.PHONY : clean clean-deps dist
clean:
rm -f *.o *~ core sr *.dump *.tar tags
clean-deps:
rm -f .*.d
dist-clean: clean clean-deps
rm -f .*.swp sr_stub.tar.gz
dist: dist-clean
(cd ..; tar -X stub/exclude -cvf sr_stub.tar stub/; gzip sr_stub.tar); \
mv ../sr_stub.tar.gz .
tags:
ctags *.c
submit:
@tar -czf router-submit.tar.gz $(sr_SRCS) $(sr_HDRS) README Makefile
PACKS = io src
# compile all *.c in packages
# $(shell) call shell
INPUT= $(shell find $(PACKS) -name '*.c')
OBJDIR = obj
# $(patsubst pattern, replacement, text)
# replace strings
#
# $(addprefix prefix,names...)
# prepend prefix to each word in names
#
# $(notdir names)
# Extracts all but the directory-part of each file name in names.
# If the file name contains no slash, it is left unchanged.
# Otherwise, everything through the last slash is removed from it.
OBJS = $(patsubst %.c,%.o,$(addprefix $(OBJDIR)/, $(notdir $(INPUT))))
CFLAGS = -pthread -m32
INCS = -I include/
REPATH = main
all: $(OBJS)
@echo output $(wildcard obj/*)
# list : list
$(OBJS): $(INPUT)
gcc -c -o $@ $(shell find . -name $(notdir $(patsubst %.o,%.c,$@))) \
$(CFLAGS) $(INCS)
# avoid filename = clean
.PHONY: clean
clean:
rm obj/*
PACKS = io src
# variable VPATH specifies a list of directories that make should search.
# Most often, the directories are expected to contain prerequisite files
# that are not in the current directory;
# however, make uses VPATH as a search list for both prerequisites
# and targets of rules.
VPATH = io:src
# compile all *.c in packages
# $(shell) call shell
INPUT= $(shell find $(PACKS) -name '*.c')
OBJDIR = obj
# $(patsubst pattern, replacement, text)
# replace strings
#
# $(addprefix prefix,names...)
# prepend prefix to each word in names
#
# $(notdir names)
# Extracts all but the directory-part of each file name in names.
# If the file name contains no slash, it is left unchanged.
# Otherwise, everything through the last slash is removed from it.
OBJS = $(patsubst %.c,%.o,$(addprefix $(OBJDIR)/, $(notdir $(INPUT))))
CFLAGS = -pthread -m32
INCS = -I include/
REPATH = main
all: $(OBJS)
@echo output $(wildcard obj/*)
# list : list
$(OBJDIR)/%.o: %.c
gcc -c -o $@ $< $(CFLAGS) $(INCS)
# avoid filename = clean
.PHONY: clean
clean:
rm obj/*
EXECS = echoclient echoservice hostinfo
EOBJS = $(addsuffix .o, $(EXECS))
OBJS = csapp.o echo.o
CFLAGS = -pthread -m32 -g
all: $(EXECS)
# static pattern matching
# tasks: pat: pat-req
# eg:
# OBJS=(foo.o bar.o)
# $(OBJS): %.o: %.c
# $(CC) -c $< -o $@ $(CCFLAGS)
# $^
# The names of all the prerequisites, with spaces between them
$(EXECS): %: %.c $(OBJS)
gcc $^ -o $@ $(CFLAGS)
%.o: %.c
gcc -c -o $@ $< $(CFLAGS)
.PHONY: clean
clean:
rm *.o
@gnosis23
Copy link
Author

$(sr_DEPS) : %.d : %.c
    $(CC) -MM $(CFLAGS) $<  > $@

include $(sr_DEPS)

gcc -MM生成的依赖关系,然后导入makefile中, 比如

sr_main.o: src/sr_main.c lib/sr_dumper.h src/sr_router.h \
 src/sr_protocol.h src/sr_arpcache.h lib/sr_if.h src/sr_protocol.h \
 lib/sr_rt.h lib/sr_if.h

这样一旦头文件改变了就会重新编译

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment