Awesome-omni-skill modern-c-makefile
Create, analyze, or improve Makefiles for modern C/C++ projects using best practices from the gnaro project template. Use when working with C/C++ projects that need clean, maintainable build systems for creating new Makefiles, improving existing ones, understanding modern patterns, or setting up comprehensive build workflows with testing and code quality tools.
git clone https://github.com/diegosouzapw/awesome-omni-skill
T=$(mktemp -d) && git clone --depth=1 https://github.com/diegosouzapw/awesome-omni-skill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/tools/modern-c-makefile-majiayu000" ~/.claude/skills/diegosouzapw-awesome-omni-skill-modern-c-makefile && rm -rf "$T"
skills/tools/modern-c-makefile-majiayu000/SKILL.mdModern C Makefile
Overview
This skill provides guidance and templates for creating clean, maintainable Makefiles for modern C/C++ projects, based on the best practices demonstrated in the gnaro project. It helps structure build systems that integrate compilation, testing, linting, formatting, and dependency management in a clear and organized way.
Core Concepts
Understand these key Makefile concepts before implementing:
- Variables: Centralize project settings like compiler flags, directories, and tool paths
- Wildcards: Use patterns like
and*.c
to automatically find source files**/*.c - Automatic Variables: Leverage
(target),$@
(first dependency),$<
(stem),$*
(target directory)$(@D) - Phony Targets: Declare
targets for actions like.PHONY
,clean
,format
that don't produce fileslint - Conditionals: Use
/ifeq
/else
for debug/release builds or platform-specific configurationsendif - Pattern Rules: Create generic rules for compiling
to.c
files.o
Makefile Template
The complete gnaro Makefile template is available in references/gnaro_makefile.md. Key sections include:
Project Structure Variables
debug ?= 0 NAME := your-project SRC_DIR := src BUILD_DIR := build INCLUDE_DIR := include LIB_DIR := lib TESTS_DIR := tests BIN_DIR := bin
Automatic Object File Generation
OBJS := $(patsubst %.c,%.o, $(wildcard $(SRC_DIR)/*.c) $(wildcard $(LIB_DIR)/**/*.c))
Compiler and Tool Configuration
CC := clang LINTER := clang-tidy FORMATTER := clang-format CFLAGS := -std=gnu17 -D _GNU_SOURCE -D __STDC_WANT_LIB_EXT1__ -Wall -Wextra -pedantic LDFLAGS := -lm
Core Targets
: Build executable with dependencies on format, lint, and object files$(NAME)
: Pattern rule for compiling object files with directory creation$(OBJS)
: Compile and run CUnit teststest
: Run static analysis with clang-tidylint
: Apply code formatting with clang-formatformat
: Run valgrind memory checkscheck
: Install development dependencies (Debian/Ubuntu)setup
: Remove build artifactsclean
: Generate compile_commands.json for toolingbear
Customization Guide
Adapting to Your Project
-
Basic Configuration:
- Update
to your project nameNAME - Adjust directory variables to match your project structure
- Modify
for your C standard and feature requirementsCFLAGS
- Update
-
Compiler and Tools:
- Change
,CC
,LINTER
to match your installed versionsFORMATTER - For GCC projects:
CC := gcc - Adjust tool paths for non-Debian systems
- Change
-
Cross-Platform Considerations:
- Replace apt-based
target with appropriate package manager commandssetup - Use conditionals for platform-specific configurations:
ifeq ($(OS),Windows_NT) # Windows-specific settings else ifeq ($(shell uname -s),Darwin) # macOS-specific settings else # Linux-specific settings endif - Replace apt-based
-
Adding Features:
- Documentation: Add
target for Doxygen or other documentation generatorsdocs - Packaging: Add
target for creating distributable archivespackage - Installation: Add
andinstall
targets for system installationuninstall
- Documentation: Add
Common Modifications
Multiple Executables:
EXECUTABLES := app1 app2 all: $(EXECUTABLES) app1: $(APP1_OBJS) $(CC) $(CFLAGS) -o $(BIN_DIR)/$@ $^ $(LDFLAGS) app2: $(APP2_OBJS) $(CC) $(CFLAGS) -o $(BIN_DIR)/$@ $^ $(LDFLAGS)
Header Dependency Generation:
DEPFILES := $(OBJS:.o=.d) -include $(DEPFILES) %.d: %.c @$(CC) $(CFLAGS) -MM -MP -MT $*.o -MF $@ $<
Verbose Mode:
V ?= 0 ifeq ($(V),1) Q := else Q := @ endif $(OBJS): dir $(Q)mkdir -p $(BUILD_DIR)/$(@D) $(Q)$(CC) $(CFLAGS) -o $(BUILD_DIR)/$@ -c $*.c
Usage Examples
Example 1: Creating a New Makefile
User: Create a Makefile for my C project "calculator" with source files in src/, headers in include/, tests in tests/ Assistant: Creates Makefile based on template with customized variables
Example 2: Adding Testing to Existing Makefile
User: Add CUnit testing support to my existing Makefile Assistant: Adds test target and updates dependencies
Example 3: Improving Build Performance
User: My Makefile rebuilds everything when headers change Assistant: Adds automatic dependency generation with -MM flags
Example 4: Cross-Platform Support
User: Make my Makefile work on both Linux and macOS Assistant: Adds OS detection and conditional tool paths
Quick Reference
Essential Commands
make # Build project (default target) make debug=1 # Build with debug symbols, no optimization make test # Run tests make lint # Run static analysis make format # Format code make check # Run memory checks make clean # Clean build artifacts
Project Structure Convention
project/ ├── Makefile ├── src/ # Source files (*.c) ├── include/ # Header files (*.h) ├── lib/ # Third-party libraries ├── tests/ # Test files ├── build/ # Object files (generated) └── bin/ # Executables (generated)
Resources
This skill includes the following bundled resources:
references/
Reference documentation for Makefile best practices and templates:
- gnaro_makefile.md: Complete Makefile from the gnaro project with detailed analysis and adaptation notes. Use this as the primary reference template.
- best_practices.md: Comprehensive guide to modern C Makefile design patterns, advanced techniques, and common solutions.
When to load references: Read these files when you need detailed analysis of Makefile patterns, adaptation guidance, or advanced techniques beyond what's covered in the main skill.
assets/
Template files and guides for project setup:
- basic_makefile.template: Simplified Makefile template ready for customization. Replace
and adjust directories as needed.PROJECT_NAME - project_structure_example.md: Recommended project structure with variations for different project types (single-header libraries, applications with resources, multi-target projects).
- cross_platform_guide.md: Guide for adapting Makefiles to different operating systems (Linux, macOS, Windows) with platform detection, package manager integration, and cross-compilation support.
When to use assets: These files provide starting points and templates that can be copied and adapted for specific projects. They're particularly useful when creating new projects or porting existing ones to different platforms.
scripts/
This skill doesn't include scripts since Makefile creation is primarily about configuration and structure rather than automated processing. However, consider creating custom scripts for:
- Project scaffolding (generating directory structure)
- Dependency management
- Build automation beyond Makefile capabilities