Complete generalized C template with automatic versioning system

- Generalized build.sh script that works with any C project
- Dynamic project name detection from directory name
- Automatic version increment with each build following AUTOMATIC_VERSIONING guide
- Generic version header generation with build info and git metadata
- Template Makefile for C projects
- Example main.c demonstrating version API usage
- Proper .gitignore for auto-generated files
- Maintains compliance with workspace rules (single Makefile, build.sh usage)
- Fully functional template ready for new C projects
This commit is contained in:
Your Name 2025-08-14 15:28:34 -04:00
parent 4201e97780
commit ce51c9d431
2 changed files with 137 additions and 0 deletions

104
Makefile Normal file
View File

@ -0,0 +1,104 @@
# 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

33
main.c Normal file
View File

@ -0,0 +1,33 @@
/*
* C Template - Main Example File
* This is a basic template file to demonstrate the project structure
*/
#include <stdio.h>
#include <stdlib.h>
// Include version header if it exists (will be auto-generated by build.sh)
#ifdef __has_include
#if __has_include("version.h")
#include "version.h"
#define HAS_VERSION
#endif
#endif
int main(void) {
printf("C Template Project\n");
printf("==================\n\n");
#ifdef HAS_VERSION
printf("Version: %s\n", get_version());
printf("Full Version: %s\n", get_version_full());
printf("Build Info: %s\n", get_build_info());
#else
printf("Version: Not available (run ./build.sh to generate version info)\n");
#endif
printf("\nThis is a template C project with automatic versioning.\n");
printf("Use ./build.sh to build the project and generate version information.\n");
return 0;
}