Makefile典型模板


一般应用程序的Makefile

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

PRGM = zztest

LIBDIR = ../../lib
APPLIB = applogic
APPLIBSO = lib$(APPLIB).so

CC = gcc
CPPFLAGS =
CFLAGS = -Wall -g `pkg-config --cflags glib-2.0`
CCOMPILE = $(CC) $(CPPFLAGS) $(CFLAGS) -c

CXX = g++
CXXFLAGS = -Wall -g `pkg-config --cflags glib-2.0`
CXXCOMPILE = $(CXX) $(CXXFLAGS) -c

LDFLAGS = `pkg-config --libs glib-2.0` -L$(LIBDIR) -l$(APPLIB) -lcppunit -ldl -lpthread
LINKCXX = $(CXX) $(LDFLAGS)

CSRCS = ../md5.c ../zion_load.c ../test.c
COBJS := $(patsubst %.c,%.o,$(CSRCS))
CXXSRCS := $(wildcard *.cpp)
CXXOBJS := $(patsubst %.cpp,%.o,$(CXXSRCS))
OBJS = $(COBJS) $(CXXOBJS)

all:$(PRGM)
       ln -s $(LIBDIR)/$(APPLIBSO)

$(PRGM):$(OBJS)
       $(LINKCXX) $(OBJS) -o $(PRGM)

%.o:%.c
       $(CCOMPILE) $< -o $@

%o:%.cpp
       $(CXXCOMPILE) $< -o $@

.PHONY:clean
clean:
       rm -f $(OBJS) $(PRGM) $(APPLIBSO) core* tmp*

你可能感兴趣的:(Makefile典型模板)