RenHao

makefile_test

Apr 19th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 1.78 KB | None | 0 0
  1. Makefile sample, from easy to hard
  2. **********************************************************************************
  3.  
  4. #Version 1
  5. test: test.c hellof.c
  6.     gcc -o test test.c hellof.c -I.
  7.  
  8. **********************************************************************************
  9.  
  10. #V2
  11. CC=gcc
  12. CFLAGS=-I.
  13.  
  14. test: test.o hellof.o
  15.     $(CC) -o test test.o hellof.o $(CFLAGS)
  16.  
  17. **********************************************************************************
  18.  
  19. #V3
  20. CC=gcc
  21. CFLAGS=-I./Include
  22. DEPS = hellomake.h
  23.  
  24. %.o: %.c $(DEPS)
  25.     $(CC) -c -o $@ $< $(CFLAGS)
  26. ######-c: compile without linked
  27. hellomake: test.o hellof.o
  28.     gcc -o test test.o hellof.o -I.
  29.  
  30. **********************************************************************************
  31.  
  32. #V4
  33. CC=gcc
  34. CFLAGS=-I.
  35. DEPS = hellomake.h
  36. OBJ = test.o hellof.o
  37.  
  38. %.o: %.c $(DEPS)
  39.     $(CC) -c -o $@ $< $(CFLAGS)
  40.  
  41. hellomake: $(OBJ)
  42.     gcc -o $@ $^ $(CFLAGS)
  43.  
  44. **********************************************************************************
  45.  
  46. #V5
  47. IDIR =./Include
  48. CC = gcc
  49. CFLAGS = -I $(IDIR)
  50.  
  51. ODIR=./obj
  52. LDIR = ./lib
  53.  
  54. LIBS = -lm
  55.  
  56. _DEPS = hellomake.h
  57. DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
  58. #    (DEPS)=(% == _DEPS) ? $(IDIR)/% ;
  59. #   $(patsubst (item1),(item2),(dir)) ==> $(dir:%.c=%.o) ==> $(var:a=b) 或 ${var:a=b} : Replace item1 to item2 in dir
  60.  
  61. _OBJ = test.o hellof.o
  62. OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
  63.  
  64. $(ODIR)/%.o: %.c $(DEPS)
  65.     $(CC) -c  -o $@ $< $(CFLAGS)
  66.  
  67. hellomake: $(OBJ)
  68.     $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
  69.  
  70. #   $@ : to put the output of the compilation in the file named on the left side of the :
  71. #   $< :  the first item in the dependencies list
  72. #   $^ :  the left and right sides of the :
  73.  
  74. .PHONY: clean   # make clean
  75. clean:
  76.     rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
  77.  
  78.  
  79. http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/
Advertisement
Add Comment
Please, Sign In to add comment