Initial commit

This commit is contained in:
Ulysse Cura 2026-07-25 20:09:52 +02:00
commit 58c55f1e9d
4 changed files with 239 additions and 0 deletions

85
Makefile Normal file
View File

@ -0,0 +1,85 @@
# Build folder
BUILD_DIR = build
# Source files
SOURCES := \
src/main.c
# Output target name
OUTPUT := Test_SDL3
# Compiler and flags
CC = gcc
CCFLAGS = --std=c17 --pedantic -O0 -g $(shell pkg-config --cflags sdl3) \
-Wall -Wno-missing-braces -Wextra -Wno-missing-field-initializers \
-Wformat=2 -Wswitch-default -Wswitch-enum -Wcast-align \
-Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \
-Wstrict-prototypes -Winline -Wundef -Wnested-externs \
-Wcast-qual -Wshadow -Wunreachable-code -Wlogical-op \
-Wfloat-equal -Wstrict-aliasing=2 -Wredundant-decls \
-Wold-style-definition
LDFLAGS = $(shell pkg-config --libs sdl3)
# Include directories
INCLUDE_DIRS =
# Change output location
OUTPUT := $(BUILD_DIR)/$(OUTPUT)
# Deduce objects
OBJECTS = $(SOURCES:%.c=$(BUILD_DIR)/%.o)
# Verbose mode
ifeq ($(VERBOSE), 1)
Q :=
else
Q := @
endif
# Colors
GREEN := \033[32m
YELLOW := \033[33m
RESET := \033[m
# Current file nb to process
CURRENT_FILE := 0
# Default targets
all: count_build build_dir end
# Create build directory
build_dir:
$(Q)mkdir -p $(dir $(OBJECTS))
# Link target
$(OUTPUT): $(OBJECTS)
@echo -e "[100%] $(YELLOW)Linking $(OUTPUT)$(RESET)"
$(Q)$(CC) $(LDFLAGS) -o $@ $(OBJECTS)
# Build .o files from .c
$(BUILD_DIR)/%.o: %.c
$(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE)+1))))
$(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE)*100/$(TOTAL_FILES)))))
@echo -e "[$(PERCENTAGE)%] $(GREEN)Building C object $@$(RESET)"
$(Q)$(CC) $(CCFLAGS) $(INCLUDE_DIRS:%=-I%) -MMD -MP -c $< -o $@
# Source files dependencies
-include $(OBJECTS:.o=.d)
count_build:
$(eval export TOTAL_FILES := $(shell echo $$(($$(make -n $(OBJECTS) 2>/dev/null | grep -c "Building") + 1))))
# Print ending message
end: $(OUTPUT)
@echo "Built target $(OUTPUT)"
# Clean
.PHONY: clean
clean:
$(Q)rm -rf $(BUILD_DIR)
# Run executable
.PHONY: run
run: all
$(Q)./$(OUTPUT)

77
README.md Normal file
View File

@ -0,0 +1,77 @@
# GCC_Project
## Description
This is a template for **C** projects using make and gcc.
## Setup
To make this template work you will need to install make and gcc.
On Debian 13 the command is :
```bash
sudo apt install make gcc
```
## Usage
In vscode open your task runner and use the different tasks :
- **Build** : Build the current project
- **Clean** : Clean build directory
- **Run** : Run the compiled executable
- **Build Verbose** : Build with verbosity on (show the commands called by make while building the project)
They correspond to calling make with the following :
- **Build** : make
- **Clean** : make clean
- **Run** : make run
- **Verbosity on** : make ... VERBOSE=1 ; You can add verbosity to any task that you want
If you want to add more source files in your project, add their path in the SOURCES variable in the Makefile.
For exemple to add src/wifi/udp_client.c you should do something like this :
```bash
# Source files
SOURCES := \
src/main.c \
src/wifi/udp_client.c
```
You can also add include drectories for headers :
TIP : Add the same path to your c_cpp_properties.json in the .vscode folder in the includePath list, like this you don't have include errors by IntelliSense
```bash
# Include directories
INCLUDE_DIRS = lib/super_extra/include/
```
> .vscode/c_cpp_properties.json
```json
{
"configurations": [
{
"name": "Default",
"includePath": [
"lib/super_extra/include/"
]
}
],
"version": 1
}
```
You can change the name of the executable and the build dir too (don't forget to change it in the .gitignore):
```bash
# Build folder
BUILD_DIR = build_dir
#...
# Output target name
OUTPUT := BestExecOfHumanity
```
And change the compiler and linker args as you want :
```bash
# Flags
CCFLAGS = -Wall -Wextra -std=c17 -g -no-pie
LDFLAGS = -no-pie
```

19
shell.nix Normal file
View File

@ -0,0 +1,19 @@
with import <nixpkgs> {};
stdenv.mkDerivation {
name = "env";
nativeBuildInputs = [
gnumake
gcc
gdb
pkg-config
valgrind
];
buildInputs = [
sdl3
];
shellHook = ''
export LD_LIBRARY_PATH=${pkgs.sdl3}/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=${pkgs.sdl3}/lib/pkgconfig:$PKG_CONFIG_PATH
unset TEMP TMP TEMPDIR TMPDIR
'';
}

58
src/main.c Normal file
View File

@ -0,0 +1,58 @@
#include <SDL3/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void)
{
int exit_status = EXIT_FAILURE;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
{
fprintf(stderr, "Failed to initialise SDL3 : %s\n", SDL_GetError());
goto end;
}
if(!SDL_CreateWindowAndRenderer("Test_SDL3", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer))
{
fprintf(stderr, "Failed to create window or renderer : %s\n", SDL_GetError());
goto end;
}
bool running = true;
while(running)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_EVENT_QUIT)
{
running = false;
}
}
if(!SDL_RenderClear(renderer))
{
fprintf(stderr, "Failed to clear renderer : %s\n", SDL_GetError());
goto end;
}
if(!SDL_RenderPresent(renderer))
{
fprintf(stderr, "Failed to present renderer : %s\n", SDL_GetError());
goto end;
}
}
exit_status = EXIT_SUCCESS;
end:
if(renderer) SDL_DestroyRenderer(renderer);
if(window) SDL_DestroyWindow(window);
SDL_Quit();
return exit_status;
}