Makefile

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
m (Created page with '==General purpose makefile== Usage: * <code>make</code> - build the whole project * <code>make clean</code> - remove any object, binary, temporary (*~) files * <code>make spotles…')
 
m
Line 30: Line 30:
 
SRCS+=$(BINOUT)
 
SRCS+=$(BINOUT)
 
CC:=gcc
 
CC:=gcc
CFLAGS:=$(DEBUG) -c
+
CFLAGS:=$(DEBUG) -c -Wall
 
CLINKS:=$(DEBUG) $(addprefix -l,$(CLIBS))
 
CLINKS:=$(DEBUG) $(addprefix -l,$(CLIBS))
  

Revision as of 14:41, 15 October 2011

Contents

General purpose makefile

Usage:

  • make - build the whole project
  • make clean - remove any object, binary, temporary (*~) files
  • make spotless - same as clean, but also removes any directories created during the build process
  • make run - build the project, and execute it
  • make new - re-build the whole project
# enable debugging?
DEBUG:=-g
 
# the output binary name (found in ./bin)
# this is also used as a source file - %.c
BINOUT:=main
 
# when you type `make run` these args will be passed
EXECARGS:=
 
# the source files you wish to use (without the '.c')
SRCS:=
 
# any libraries you want to link with (without the 'lib'/'l' - e.g. "m rt")
CLIBS:=
 
###################################################################################
# There should be no need to change below here for most projects
 
DIRS:=bin obj
SRCS+=$(BINOUT)
CC:=gcc
CFLAGS:=$(DEBUG) -c -Wall
CLINKS:=$(DEBUG) $(addprefix -l,$(CLIBS))
 
.PHONY: all new clean run spotless
 
all: .dirs ./bin/$(BINOUT)
 
new: clean ./bin/$(BINOUT)
 
clean:
	rm -f *~
	rm -f ./bin/$(BINOUT)
	rm -f ./obj/*.o
 
run: ./bin/$(BINOUT)
	./bin/$(BINOUT) $(EXECARGS)
 
spotless: clean
	rm -rdf $(addprefix ./,$(DIRS)) .dirs
.dirs: makefile
	mkdir -p $(addprefix ./,$(DIRS))
	@touch .dirs
 
 
./bin/$(BINOUT): $(addprefix ./obj/,$(addsuffix .o,$(SRCS))) makefile
	$(CC) $(CLINKS) $(filter %.o,$^) -o $@
 
./obj/%.o: %.c %.h makefile
	$(CC) $(CFLAGS) $*.c -o $@
./obj/%.o: %.c makefile
	$(CC) $(CFLAGS) $*.c -o $@

Useful Rules

Timestamp your binaries

File: makefile

.PHONY stamp
 
stamp:
	sed -i -re "s/^(.*compileStamp[^=]*)=[ ]*[0-9]*(.*)$$/\1= `date +%s`\2/" stamp.c
 
./obj/stamp.o: makefile stamp ./stamp.c
	$(CC) $(CFLAGS) ./stamp.c -o $@

File: stamp.c

const char *svnRevision = "$Id$";  /* holds nice SVN info, this file should ALWAYS get comitted */
unsigned int compileStamp = 0;     /* holds the unix timestamp of the most recent compile */

Provide an 'install' rule

LIBOUT:=libName
VERSION:=1.0.0
SYSHEADERS:=mylib
 
USRLIB:=/usr/lib
USRINC:=/usr/include
 
.PHONY: install install_su
 
install: all
	@echo
	@echo
ifneq ($(shell echo $$USER),root)
	@echo "#######################################################################################"
	@echo "### To Install this library I need the root password please!"
	@echo "#######################################################################################"
endif
	su -c "make install_su --no-print-directory"
	@echo
 
install_su: $(USRLIB)/$(LIBOUT).so.$(VERSION) $(addprefix $(USRINC)/,$(addsuffix .h,$(SYSHEADERS)))
$(USRLIB)/$(LIBOUT).so.$(VERSION): ./lib/$(LIBOUT).so.$(VERSION)
	cp -f ./lib/$(LIBOUT).so.$(VERSION) $(USRLIB)/$(LIBOUT).so.$(VERSION)
	@chown root:root $(USRLIB)/$(LIBOUT).so.$(VERSION)
	@chmod 755 $(USRLIB)/$(LIBOUT).so.$(VERSION)
	ln -sf ./$(LIBOUT).so.$(VERSION) $(USRLIB)/$(LIBOUT).so.1
	@chown root:root $(USRLIB)/$(LIBOUT).so.1
	@chmod 755 $(USRLIB)/$(LIBOUT).so.1
	ln -sf ./$(LIBOUT).so.$(VERSION) $(USRLIB)/$(LIBOUT).so
	@chown root:root $(USRLIB)/$(LIBOUT).so
	@chmod 755 $(USRLIB)/$(LIBOUT).so
 
$(USRINC)/%.h: %.h
	cp -f $*.h $@
	@chown root:root $@
	@chmod 644 $@
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox