I've established a standardized project structure to maintain consistency and efficiency across all my work. This foundation ensures coherence while significantly reducing setup time for new projects.
You can explore my template structure on Github, though this approach works equally well on other development platforms.
I will present to you the contents of my Makefile and the github workflow which defines the CI pipeline runs when code changes and should make sure all of your changes work with the rest of the code when it's integrated. And finally I show you how to create a template on github.
Content of my template
Makefile
Github Workflow
This GitHub Actions pipeline runs on push or pull requests to the main branch. It uses an Ubuntu environment to clone the repository, install essential build tools, and then format the code with the make format command.
A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.
MIT LICENSE
Setup Your Github Template
Create a GitHub repository named "random-template."
Add the content that will make up your template to the repository. Commit and push your changes.
Go to the repository's Settings and check the box labeled "Template repository."
Now, anyone (including you) can generate a new repository using this template.
CC = clang
# The -MMD flag automatically generates Makefile dependencies for source files, tracking
# changes in included headers to enable efficient recompilation.
# The -MP flag tells the compiler to add dummy targets for each dependency,
# thus avoiding make errors if header files are removed.
CFLAGS = -Wall -Wextra -Iinclude -MMD -MP
VALGRINDFLAGS = --leak-check=full --show-leak-kinds=all
SRC_DIR = src
BIN_DIR = bin
OBJ_DIR = obj
SRCS = $(wildcard $(SRC_DIR)/*.c)
# patsubst (pattern substitution) replaces a given pattern in a list.
# $(patsubst <pattern_a>, <pattern_b>, <list>)
OBJS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS))
DEPENDS = $(OBJS:.o=.d)
TARGET = $(BIN_DIR)/prog
DEP_FILES = $(DEPENDS)
all: $(TARGET)
$(TARGET): $(OBJS) | $(BIN_DIR)
@mkdir -p $(BIN_DIR)
$(CC) $(CFLAGS) -o $@ $^
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c -o $@ $<
$(BIN_DIR) $(OBJ_DIR):
@mkdir -p $@
-include $(DEP_FILES)
clean:
rm -rf $(OBJ_DIR) $(BIN_DIR)
format:
find . -name '*.c' -o -name '*.h' | xargs clang-format -i
debug: CFLAGS += -g -DDEBUG
debug: clean all
leaks: debug
valgrind $(VALGRINDFLAGS) ./$(TARGET)
run: $(TARGET)
./$(TARGET)
.PHONY: all clean format debug run leaks
name: Project pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install dependencies
run: sudo apt-get install -y build-essential
- name: Compile code
run: make format
MIT License
Copyright (c) 2024 Ammon SCHIFFER
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.