生成可执行程序
PRGM = zztest
CC = gcc
CPPFLAGS =
LDFLAGS = -lrt -lz
CFLAGS = -Wall -ggdb3
COMPILE = $(CC) $(CPPFLAGS) $(CFLAGS) -c
LINKCC = $(CC) $(LDFLAGS)
LIBA = libcshm.a
SRCS := $(wildcard *.c)
DEPS := $(patsubst %.c,%.d,$(SRCS))
OBJS := $(patsubst %.c,%.o,$(SRCS))
all:$(PRGM)
$(PRGM):$(OBJS) $(DEPS)
$(LINKCC) $(OBJS) $(LIBA) -o $(PRGM)
%.d:%.c
$(CC) -MM $(CPPFLAGS) $< > $@
%.o:%.c
$(COMPILE) $< -o $@
.PHONY:clean
clean:
rm -f $(OBJS) $(DEPS) $(PRGM)
explain:
@echo "The information represents in the program:"
@echo "Final executable name: $(PRGM)"
@echo "Source files: $(SRCS)"
@echo "Object files: $(OBJS)"
depend:$(DEPS)
@echo "Dependencies are now up-to-date"
-include $(DEPS)
生成动态库的Makefile
- CC = gcc
- CFLAGS = -Wall -O2 `pkg-config --cflags glib-2.0`
- LIBCFLAGS = $(CFLAGS) -D_REENTRANT -fPIC
- LIBSRCS := $(wildcard *.c)
- LIBOBJS := $(patsubst %.c,%.o,$(LIBSRCS))
- LIBRARY = libpustation.so.1.0.0
- SONAME = libpustation.so.1
- LIBNAME = libpustation.so
- APPLIB = applogic
- APPLIBSO = $(APPLIB).so
- APPLIBDIR = ../lib
- APPLIBNAME = lib$(APPLIBSO)
- all:$(LIBRARY)
- cp $(LIBNAME) $(APPLIBDIR)/$(APPLIBNAME)
- cp $(LIBNAME) $(APPLIBDIR)/$(APPLIBSO)
- $(LIBRARY):$(LIBOBJS)
- $(CC) -shared -W1,-soname,$(SONAME) $(LIBOBJS) -o $@
- ln -sf $@ $(LIBNAME)
- ln -sf $@ $(SONAME)
- $(LIBOBJS):%.o:%.c
- $(CC) $(LIBCFLAGS) -c $< -o $@
- .PHONY:clean distclean
- clean:
- rm -f $(LIBOBJS)
- distclean:
- rm -f $(LIBOBJS) $(LIBRARY) $(SONAME) $(LIBNAME) $(APPLIBDIR)/$(APPLIBNAME) $(APPLIBDIR)/$(APPLIBSO)
生成静态库的Makefile
- LIBA = libstatic.a
- AR = ar
- CC = gcc
- CPPFLAGS =
- CFLAGS = -O2
- COMPILE = $(CC) $(CPPFLAGS) $(CFLAGS) -c
- SRCS := $(wildcard *.c)
- OBJS := $(patsubst %.c,%.o,$(SRCS))
- all:$(LIBA)
- $(LIBA):$(OBJS)
- $(AR) cqs $(LIBA) $(OBJS)
- %.o:%.c
- $(COMPILE) $< -o $@
- .PHONY:clean distclean
- clean:
- rm -f $(OBJS)
- distclean:
- rm -f $(OBJS) $(LIBA)
C与C++混合编译的Makefile