Advertisement
okpalan

common-makefile.mk

Nov 9th, 2023 (edited)
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 1.42 KB | Source Code | 0 0
  1. #    C Standard MakeFile for Small Static Libraries and Project:To use with:1) Linux: sudo apt install make make clean make all2) windows #      scoop install make make clean make all
  2.  
  3.     CC=gcc
  4.     AR=ar
  5.     AR_FLAGS=rcs
  6.  
  7.     SRC_DIR=src
  8.     INCLUDE_DIR=include
  9.     CFLAGS= -Wall -Wextra -Wpedantic -I$(INCLUDE_DIR) -MMD -std=c99 -lm
  10.  
  11.     # Use wildcard to find all .c files
  12.     # in the SRC_DIR
  13.     SOURCES := $(wildcard $(SRC_DIR)/*.c)
  14.  
  15.     OBJ_DIR=obj
  16.     # Alternative syntax to generate object file
  17.     # names from source file names
  18.     OBJECTS := $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
  19.  
  20.     DEPS_DIR=./deps
  21.  
  22.     # Include dependency files
  23.     # generated by -MMD flag.
  24.     DEPSFILES := $(OBJECTS:.o=.d)
  25.  
  26.     LIB_DIR=lib
  27.     LIB=$(LIB_DIR)/libstencil.a
  28.  
  29.  
  30.     all: $(LIB)
  31.  
  32.     # Include the dependency files
  33.     -include $(DEPSFILES)
  34.  
  35.     $(LIB): $(OBJECTS)
  36.         mkdir -p $(LIB_DIR)
  37.         $(AR) $(AR_FLAGS) $@ $(OBJECTS)
  38.  
  39.     # Pattern rule to generate object files
  40.     # from source files
  41.     $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
  42.         mkdir -p $(OBJ_DIR)
  43.         $(CC) $(CFLAGS) -c $< -o $@
  44.  
  45.     clean:
  46.         rm -rf $(OBJ_DIR) $(LIB_DIR)
  47.  
  48.     .PHONY: all clean
  49.  
  50.  
  51.  
  52.     # Move the dependency files after object files are created
  53.     post-build:
  54.         mkdir -p $(DEPS_DIR)
  55.         mv $(DEPSFILES) $(DEPS_DIR)
  56.  
  57.     # Ensure that 'post-build' is run after the
  58.     # library is built
  59.     $(LIB): post-build
  60.  
Tags: makefile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement