commit e9aea0dee79c1987fc7d63cd0f6841a490fa3cf6 Author: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Mon Jan 12 18:38:38 2026 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..253c543 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +a.out +test.out +.build/ + +*.o +*.d +*~ +*.swp +*.swo +*.bak +*.log + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e39acfc --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 lohhiiccc + +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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0584053 --- /dev/null +++ b/Makefile @@ -0,0 +1,71 @@ +NAME = a.out +TEST_BIN= test.out + +CMD = c-md + +.DEFAULT_GOAL := all +MAKEFLAGS += --no-print-directory +include sources.mk + +CC = clang +CPPFLAGS = -std=c99 -I includes +CFLAGS = -Wall -Wextra -Werror -pipe +LDFLAGS = +TEST_LDFLAGS = -lcriterion + + +BUILD_DIR = .build +OBJ_DIR = $(BUILD_DIR)/objs +MAP_DIR = $(BUILD_DIR)/maps + +GEN_SRCS = $(MD_SRCS:$(SRC_DIR)/%.c.md=$(BUILD_DIR)/srcs/%.c) +GEN_HDRS = $(MD_HDRS:$(INC_DIR)/%.h.md=$(BUILD_DIR)/$(INC_DIR)/%.h) + +OBJS = $(GEN_SRCS:$(BUILD_DIR)/srcs/%.c=$(OBJ_DIR)/%.o) +DEPS = $(OBJS:.o=.d) + +.SECONDARY: $(GEN_SRCS) $(GEN_HDRS) + +.PHONY: all +all: $(NAME) + +$(NAME): $(GEN_HDRS) $(OBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) + +$(BUILD_DIR)/srcs/%.c: $(SRC_DIR)/%.c.md + @mkdir -p $(dir $@) $(dir $(MAP_DIR)/$*.map) + $(CMD) -i $< -o $@ -m $(MAP_DIR)/$*.map + +$(BUILD_DIR)/$(INC_DIR)/%.h: $(INC_DIR)/%.h.md + @mkdir -p $(dir $@) $(dir $(MAP_DIR)/$*.h.map) + $(CMD) -e c -i $< -o $@ -m $(MAP_DIR)/$*.h.map + +$(OBJ_DIR)/%.o: $(BUILD_DIR)/srcs/%.c $(GEN_HDRS) + @mkdir -p $(dir $@) + $(CC) $(CPPFLAGS) $(CFLAGS) -I $(BUILD_DIR)/$(INC_DIR) -MMD -MP -c $< -o $@ + +-include $(DEPS) + + +.PHONY: test +test: $(TEST_BIN) + ./$(TEST_BIN) + +$(TEST_BIN): $(TESTS) $(filter-out $(OBJ_DIR)/main.o,$(OBJS)) + @mkdir -p $(dir $@) + $(CC) $(CPPFLAGS) $(CFLAGS) -I $(BUILD_DIR)/$(INC_DIR) $(TEST_LDFLAGS) $^ -o $@ + + +.PHONY: clean +clean: + $(RM) -r $(OBJ_DIR) + +.PHONY: fclean +fclean: clean + $(RM) $(NAME) + $(RM) $(TEST_BIN) + +.PHONY: re +re: fclean + $(MAKE) all + diff --git a/README.md b/README.md new file mode 100644 index 0000000..bdb4810 --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# c-md-template + +A minimal, modern C project template using +[c-md](https://github.com/lohhiiccc/c-md) for literate programming. Features a +portable Makefile, clear directory structure, and unit testing with Criterion. + +## Features + +- Literate source files (`.c.md` and `.h.md`) extracted automatically using the + `c-md` transpiler +- Modern portable Makefile with full dependency handling +- Example structure: `srcs/`, `includes/`, `tests/` +- Unit test setup with [Criterion](https://github.com/Snaipe/Criterion) +- Common targets: `all`, `clean`, `fclean`, `re`, `test` +- Easily extensible for larger projects + +## Requirements + +- C compiler (GCC or Clang recommended) +- [Criterion](https://github.com/Snaipe/Criterion) for unit testing +- [c-md](https://github.com/ton-livre/c-md) transpiler + +## Directory Structure + +``` +. +|-- includes/ # Header files (.h.md) +|-- srcs/ # Source files (.c.md) +|-- tests/ # Unit test files (plain C) +|-- Makefile +|-- sources.mk +``` + +## Usage + +### Build and Test + +```sh +make # Transpile and compile the project (outputs a.out) +make test # Build and run unit tests +make clean # Remove object files +make fclean # Remove all build artifacts and binaries +make re # Full rebuild +``` + +Add your source and header files in `sources.mk` to scale the project. + +## License + +[MIT](LICENSE) diff --git a/includes/utils.h.md b/includes/utils.h.md new file mode 100644 index 0000000..1676b93 --- /dev/null +++ b/includes/utils.h.md @@ -0,0 +1,29 @@ +# utils.h +Utility functions for the project + +## Include Guard + +```c +#ifndef UTILS_H +# define UTILS_H +``` + +## Function declarations + +### [`add`](/srcs/utils.c.md) +```c +int +add(int a, int b); +``` + +### [`subtract`](/srcs/utils.c.md) +```c +int +subtract(int a, int b); +``` + +## End Guard + +```c +#endif +``` diff --git a/sources.mk b/sources.mk new file mode 100644 index 0000000..50ada95 --- /dev/null +++ b/sources.mk @@ -0,0 +1,10 @@ + +SRC_DIR = srcs +MD_SRCS = $(SRC_DIR)/main.c.md \ + $(SRC_DIR)/utils.c.md + +INC_DIR = includes +MD_HDRS = $(INC_DIR)/utils.h.md \ + +TESTS_DIR = tests +TESTS = $(TESTS_DIR)/test_main.c \ diff --git a/srcs/main.c.md b/srcs/main.c.md new file mode 100644 index 0000000..7ebec01 --- /dev/null +++ b/srcs/main.c.md @@ -0,0 +1,17 @@ +# main.c +Basic C program structure + +## Includes +```c +#include "utils.h" +``` + +## Main Function + +```c +int +main() +{ + return subtract(add(42, 2), 2); +} +``` diff --git a/srcs/utils.c.md b/srcs/utils.c.md new file mode 100644 index 0000000..68738c4 --- /dev/null +++ b/srcs/utils.c.md @@ -0,0 +1,18 @@ +# utils.c +Utility functions for the project + +```c +int +add(int a, int b) +{ + return a + b; +} +``` + +```c +int +subtract(int a, int b) +{ + return a - b; +} +``` diff --git a/tests/test_main.c b/tests/test_main.c new file mode 100644 index 0000000..00354d3 --- /dev/null +++ b/tests/test_main.c @@ -0,0 +1,11 @@ +#include + +#include "utils.h" + +Test(add, addition_function) +{ + int a = 2; + int b = 3; + int result = add(a, b); + cr_assert_eq(result, 5, "Expected %d but got %d", 5, result); +}