# Generic C Project Makefile Template # This is a basic template - customize for your specific project needs PROJECT_NAME = c_template CC = gcc CFLAGS = -Wall -Wextra -std=c99 -O2 LIBS = -lm # Detect source directory SRC_DIR = $(shell if [ -d "src" ]; then echo "src"; elif [ -d "lib" ]; then echo "lib"; else echo "."; fi) # Source files (add your actual source files here) # Note: version.c will be auto-generated by build.sh SOURCES = $(wildcard $(SRC_DIR)/*.c) OBJECTS = $(SOURCES:.c=.o) LIBRARY = lib$(PROJECT_NAME).a # Default target all: $(LIBRARY) # Build static library $(LIBRARY): $(OBJECTS) ar rcs $@ $^ @echo "Built $@ successfully" # Build object files %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ # Build examples (if examples directory exists) examples: $(LIBRARY) @if [ -d "examples" ]; then \ echo "Building examples..."; \ for example in examples/*.c; do \ if [ -f "$$example" ]; then \ target=$${example%.c}; \ $(CC) $(CFLAGS) "$$example" -L. -l$(PROJECT_NAME) $(LIBS) -o "$$target"; \ echo "Built $$target"; \ fi; \ done; \ else \ echo "No examples directory found"; \ fi # Run tests (if tests exist) test: $(LIBRARY) @if [ -d "tests" ] && [ -f "tests/Makefile" ]; then \ echo "Running tests with tests/Makefile..."; \ $(MAKE) -C tests test; \ elif [ -d "tests" ]; then \ echo "Building and running tests..."; \ for test in tests/*.c; do \ if [ -f "$$test" ]; then \ target=$${test%.c}; \ $(CC) $(CFLAGS) "$$test" -L. -l$(PROJECT_NAME) $(LIBS) -o "$$target"; \ echo "Built $$target"; \ fi; \ done; \ else \ echo "No tests found"; \ fi # Install to system install: $(LIBRARY) @echo "Installing $(LIBRARY) to /usr/local/lib/" sudo mkdir -p /usr/local/lib sudo cp $(LIBRARY) /usr/local/lib/ @if [ -f "$(SRC_DIR)/$(PROJECT_NAME).h" ]; then \ echo "Installing header file..."; \ sudo mkdir -p /usr/local/include; \ sudo cp $(SRC_DIR)/$(PROJECT_NAME).h /usr/local/include/; \ fi # Uninstall from system uninstall: @echo "Removing $(LIBRARY) from /usr/local/lib/" sudo rm -f /usr/local/lib/$(LIBRARY) @if [ -f "/usr/local/include/$(PROJECT_NAME).h" ]; then \ sudo rm -f /usr/local/include/$(PROJECT_NAME).h; \ fi # Clean build artifacts clean: rm -f $(OBJECTS) $(LIBRARY) *.o *.a *.so rm -f $(SRC_DIR)/version.h $(SRC_DIR)/version.c @if [ -d "examples" ]; then \ find examples -type f -executable -delete; \ fi @if [ -d "tests" ]; then \ find tests -type f -executable -delete; \ fi # Help target help: @echo "Available targets:" @echo " all - Build the library (default)" @echo " examples - Build example programs" @echo " test - Run tests" @echo " install - Install library to /usr/local" @echo " uninstall - Remove library from /usr/local" @echo " clean - Remove build artifacts" @echo " help - Show this help message" .PHONY: all examples test install uninstall clean help