From 58c55f1e9d3e0fbe64ba815c6ac53935e5670e83 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Sat, 25 Jul 2026 20:09:52 +0200 Subject: [PATCH] Initial commit --- Makefile | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 19 ++++++++++++ src/main.c | 58 +++++++++++++++++++++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 Makefile create mode 100644 README.md create mode 100644 shell.nix create mode 100644 src/main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7afd4eb --- /dev/null +++ b/Makefile @@ -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) diff --git a/README.md b/README.md new file mode 100644 index 0000000..939d3d3 --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..a18b96d --- /dev/null +++ b/shell.nix @@ -0,0 +1,19 @@ +with import {}; +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 + ''; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..d8c8fb9 --- /dev/null +++ b/src/main.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +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; +}