makefile 和 configure 模板

第一次写makefile和configure, 记录下模板:

makefile:

SRCDIR = src
OBJDIR = obj

CC = gcc
SHARED = -shared
FPIC = -fPIC -c

H_OBJECT = $(SRCDIR)/hashtable.h $(SRCDIR)/murmur.h

MURMUR = -D__WITH_MURMUR 
CFLAGS    = -Wall -Wextra $(dbg)-DDEBUG -DTEST $(MURMUR)
LFLAGS    = -lrt -L. -lhashtable

hashtable-obj = $(OBJDIR)/hashtable.o $(OBJDIR)/murmur.o
test-obj = $(OBJDIR)/main.o

all: mk libhashtable.so test

$(OBJDIR)/%.o: $(SRCDIR)/%.c 
    $(CC) $(CFLAGS) $(FPIC) -o $@ $<

libhashtable.so: $(hashtable-obj)
    $(CC) $(CFLAGS) $^ $(SHARED) -rdynamic -fPIC -o $@

test: $(test-obj)
    $(CC) $(CFLAGS) $(LFLAGS) $< -o $@

without_murmur:
    $(MAKE) MURMUR = all

mk: 
    mkdir -p $(OBJDIR)

#install and uninstall
.PHONY: install
install: libhashtable.so
    mkdir -p $(PREFIX)/lib
    mkdir -p $(PREFIX)/include/hashtable
    cp $< $(PREFIX)/lib/libhashtable.so
    cp $(SRCDIR)/hashtable.h $(PREFIX)/include/hashtable
    cp $(SRCDIR)/murmur.h $(PREFIX)/include/hashtable
    cp $(SRCDIR)/hashfunc.h $(PREFIX)/include/hashtable

.PHONY: uninstall
uninstall:
    rm -f $(PREFIX)/lib/libhashtable.so
    rm -rf $(PREFIX)/include/hashtable

clean:
    rm -f $(OBJDIR)/*.o
    rm -f hashtable-test
    rm -f libhashtable.so
    rm -f test
    rm -rf obj

  

configure:

#!/bin/sh

if [[ -a Makefile ]]; then
echo "delete Makefile ..."
rm -f Makefile
fi

prefix=/usr/local
debugsym=false

for arg in "$@"; do
case "$arg" in
--prefix=*)
prefix=`echo $arg | sed 's/--prefix=//'`
;;

--enable-debug)
debugsym=true;;
--disable-debug)
debugsym=false;;

--help)
echo 'usage: ./configure [options]'
echo 'options'
echo ' --prefix=<path>: installation prefix'
echo ' --enble-debug: include debug symbols'
echo ' --enble-debug: do not include debug symbols'
echo 'all invalid options are silently ignored'
exit 0
;;
esac
done

echo 'generationg makefile ...'
echo "PREFIX = $prefix" >> Makefile

if $debugsym; then
echo 'dbg = -g' >> Makefile
fi

cat Makefile.in >> Makefile
echo 'cofiguration complete, type make to build.'

  

猜你喜欢

转载自www.cnblogs.com/kouruyi/p/8966647.html