Makefile 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # In make files we always define a rule in the following form:
  2. # <output file>: <dependencies>
  3. # TAB <command for producing output file from dependencies>
  4. # In makefiles we have to use tabs for indentation. Otherwise we will get the
  5. # error: "Makefile:...: *** missing separator. Stop."
  6. INCLUDE_DIR =./include
  7. CC=gcc
  8. CFLAGS=-I$(INCLUDE_DIR)
  9. SOURCE_DIR=src
  10. # define directory name for object files
  11. OBJECT_DIR=src/obj
  12. # local libraries are in a subdirectory called lib
  13. LIB_DIR =./lib
  14. # link math library
  15. LIBS=-lm
  16. # TODO: explain the following
  17. # perhaps pathsubst is only for concattenating paths for an arbitrary number of files?
  18. _DEPS = hellomake.h
  19. DEPS = $(patsubst %,$(INCLUDE_DIR)/%,$(_DEPS))
  20. # define object files locations
  21. _OBJ = hellomake.o hellofunc.o
  22. OBJ = $(patsubst %,$(OBJECT_DIR)/%,$(_OBJ))
  23. # all object files depend on their .c files and all depend on DEPS
  24. # $@ says to put the output of the compilation result to the file named on the left side of the ":"
  25. # $< is the first item in the dependencies list
  26. $(OBJECT_DIR)/%.o: $(SOURCE_DIR)/%.c $(DEPS)
  27. $(CC) -c -o $@ $< $(CFLAGS)
  28. # hellomake consists of the object files, which according to the first rules
  29. # depend on their corresponding .c files
  30. # $@: the left hand side of the colon
  31. # $^: the right hand side of the colon
  32. hellomake: $(OBJ)
  33. $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
  34. # declare clean as a rule, to prevent make from thinking it is on output file
  35. .PHONY: clean
  36. # clean cleans up files
  37. clean:
  38. rm -f $(OBJECT_DIR)/*.o *~ core $(INCDIR)/*~