Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Makefile sample, from easy to hard
- **********************************************************************************
- #Version 1
- test: test.c hellof.c
- gcc -o test test.c hellof.c -I.
- **********************************************************************************
- #V2
- CC=gcc
- CFLAGS=-I.
- test: test.o hellof.o
- $(CC) -o test test.o hellof.o $(CFLAGS)
- **********************************************************************************
- #V3
- CC=gcc
- CFLAGS=-I./Include
- DEPS = hellomake.h
- %.o: %.c $(DEPS)
- $(CC) -c -o $@ $< $(CFLAGS)
- ######-c: compile without linked
- hellomake: test.o hellof.o
- gcc -o test test.o hellof.o -I.
- **********************************************************************************
- #V4
- CC=gcc
- CFLAGS=-I.
- DEPS = hellomake.h
- OBJ = test.o hellof.o
- %.o: %.c $(DEPS)
- $(CC) -c -o $@ $< $(CFLAGS)
- hellomake: $(OBJ)
- gcc -o $@ $^ $(CFLAGS)
- **********************************************************************************
- #V5
- IDIR =./Include
- CC = gcc
- CFLAGS = -I $(IDIR)
- ODIR=./obj
- LDIR = ./lib
- LIBS = -lm
- _DEPS = hellomake.h
- DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
- # (DEPS)=(% == _DEPS) ? $(IDIR)/% ;
- # $(patsubst (item1),(item2),(dir)) ==> $(dir:%.c=%.o) ==> $(var:a=b) 或 ${var:a=b} : Replace item1 to item2 in dir
- _OBJ = test.o hellof.o
- OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
- $(ODIR)/%.o: %.c $(DEPS)
- $(CC) -c -o $@ $< $(CFLAGS)
- hellomake: $(OBJ)
- $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
- # $@ : to put the output of the compilation in the file named on the left side of the :
- # $< : the first item in the dependencies list
- # $^ : the left and right sides of the :
- .PHONY: clean # make clean
- clean:
- rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
- http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/
Advertisement
Add Comment
Please, Sign In to add comment