Makefile
m |
m |
||
Line 2: | Line 2: | ||
[[makefile_lib|Makefile for building a system library]]<br> | [[makefile_lib|Makefile for building a system library]]<br> | ||
[[makefile_simple|Super simple makefile]] | [[makefile_simple|Super simple makefile]] | ||
+ | |||
+ | ==Super-Simple makefile== | ||
+ | <source lang="makefile"> | ||
+ | CROSS_COMPILE?= | ||
+ | CFLAGS+=-g | ||
+ | CLINKS+=-g | ||
+ | |||
+ | #--- no more configurables ---# | ||
+ | |||
+ | SRCS:=$(wildcard *.c) | ||
+ | HDRS:=$(wildcard *.h) | ||
+ | OBJS:=$(SRCS:%.c=%.o) | ||
+ | BINOUT:=main | ||
+ | GCC:=$(CROSS_COMPILE)gcc | ||
+ | |||
+ | #--- phony targets ---# | ||
+ | |||
+ | all: $(BINOUT) | ||
+ | |||
+ | run: $(BINOUT) | ||
+ | $(shell readlink -f $<) | ||
+ | |||
+ | new: clean | ||
+ | @$(MAKE) --no-print-directory | ||
+ | |||
+ | clean: | ||
+ | rm -rf $(OBJS) $(BINOUT) | ||
+ | |||
+ | #--- build system ---# | ||
+ | |||
+ | main: $(OBJS) | ||
+ | $(GCC) $(filter %.o,$(OBJS)) -o $@ | ||
+ | |||
+ | $(OBJS): %.o: %.c $(SRCS) $(HDRS) | ||
+ | $(GCC) $< -c -o $@ | ||
+ | </source> | ||
==General purpose makefile== | ==General purpose makefile== |
Revision as of 17:04, 24 May 2012
Makefile suitable for use with AVRs
Makefile for building a system library
Super simple makefile
Contents |
Super-Simple makefile
GeSHi Error: GeSHi could not find the language makefile (using path /home/www/attie.co.uk/wiki/w/extensions/SyntaxHighlight_GeSHi/geshi/geshi/) (code 2)
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
abap, actionscript, actionscript3, ada, apache, applescript, apt_sources, asm, asp, autoit, bash, basic4gl, blitzbasic, bnf, boo, c, c_mac, caddcl, cadlisp, cfdg, cfm, cil, cobol, cpp, cpp-qt, csharp, css, d, delphi, diff, div, dos, dot, eiffel, fortran, freebasic, genero, gettext, glsl, gml, gnuplot, groovy, haskell, html4strict, idl, ini, inno, io, java, java5, javascript, kixtart, klonec, klonecpp, latex, lisp, lotusformulas, lotusscript, lua, m68k, matlab, mirc, mpasm, mxml, mysql, nsis, objc, ocaml, ocaml-brief, oobas, oracle8, pascal, per, perl, php, php-brief, plsql, powershell, python, qbasic, rails, reg, robots, ruby, sas, scala, scheme, sdlbasic, smalltalk, smarty, sql, tcl, text, thinbasic, tsql, vb, vbnet, verilog, vhdl, visualfoxpro, winbatch, xml, xorg_conf, xpp, z80
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 .dirs 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 $@
Just compile everything
Replace the SRCS:=
line with this:
SRCS:=$(wildcard *.c) SRCS:=$(SRCS:.c=)
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 $@