Advertisement
sailorbob74133

Generic Makefile for Linux

Sep 10th, 2011
1,973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 2.09 KB | None | 0 0
  1. # Change the program name here, the := syntax overwrites any previous value the variable may have
  2. program_NAME := myProgram
  3.  
  4. # This line fetches the names of all the .c files in the current directory
  5. # and assigns them as a space separated list to the variable program_SRCS
  6. program_SRCS := $(wildcard *.c)
  7.  
  8. # This copies the filenames changing the .c extension to .o
  9. program_OBJS := ${program_SRCS:.c=.o}
  10.  
  11. # When you type make clean this is the list of files which will be deleted
  12. # The += syntax appends to the variable
  13. clean_list += $(program_OBJS) $(program_NAME)
  14.  
  15. # C Preprocessor Flags
  16. CPPFLAGS +=
  17. # compiler flags
  18. CFLAGS += -ansi -Wall -Wextra -pedantic-errors
  19. # libraries to link to ( m == math )
  20. program_LIBRARIES := m
  21. # LDFLAGS is the variable to hold linker flags
  22. LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))
  23.  
  24. # .PHONY means that these build targets are dummies, i.e. they don't generate any output files.
  25. # This is typically used to run shell commands.
  26. .PHONY: all clean indent
  27.  
  28. all: $(program_NAME)
  29.  
  30. clean:
  31.     @- $(RM) $(clean_list)
  32.  
  33. # Generate dependencies for all files in project
  34. # http://stackoverflow.com/questions/2801532/make-include-directive-and-dependency-generation-with-mm
  35. %.d: $(program_SRCS)
  36.     @ $(CC) $(CPPFLAGS) -MM $*.c | sed -e 's@^\(.*\)\.o:@\1.d \1.o:@' > $@
  37.  
  38. # add the list of dependancy files to the clean_list
  39. clean_list += ${program_SRCS:.c=.d}
  40.  
  41. # This is the line which actually compiles your program.
  42. $(program_NAME): $(program_OBJS)
  43.     $(LINK.c) $(program_OBJS) -o $(program_NAME)
  44.  
  45. # If you type 'make indent' this will format all your source files.
  46. # type 'sudo apt-get install indent' at the command prompt to install indent
  47. indent:
  48.     indent -linux -brf $(program_SRCS)
  49.  
  50. # type 'make test' to run a test.
  51. # for example this runs your program with jackjill.txt as input
  52. # and redirects the stdout to the file jackjill.out
  53. test: $(program_NAME)
  54.     ./$(program_NAME) jackjill.txt > jackjill.out
  55.  
  56. ifneq "$(MAKECMDGOALS)" "clean"
  57. # Include the list of dependancies generated for each object file
  58. -include ${program_SRCS:.c=.d}
  59. endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement