Initial commit

This commit is contained in:
lohhiiccc 2026-01-12 18:38:38 +01:00
commit e9aea0dee7
9 changed files with 239 additions and 0 deletions

12
.gitignore vendored Normal file
View file

@ -0,0 +1,12 @@
a.out
test.out
.build/
*.o
*.d
*~
*.swp
*.swo
*.bak
*.log

21
LICENSE Normal file
View file

@ -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.

71
Makefile Normal file
View file

@ -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

50
README.md Normal file
View file

@ -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)

29
includes/utils.h.md Normal file
View file

@ -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
```

10
sources.mk Normal file
View file

@ -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 \

17
srcs/main.c.md Normal file
View file

@ -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);
}
```

18
srcs/utils.c.md Normal file
View file

@ -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;
}
```

11
tests/test_main.c Normal file
View file

@ -0,0 +1,11 @@
#include <criterion/criterion.h>
#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);
}