Last active
June 26, 2026 20:38
-
-
Save carlosgrillet/00c212cb0e0518279b1afa69e9ae20d0 to your computer and use it in GitHub Desktop.
My small script to create new simple C projects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| if [[ $# -eq 0 ]]; then | |
| echo "Error: Please provide a project name." >&2 | |
| echo "Usage: $0 <name>" >&2 | |
| exit 1 | |
| fi | |
| project_name="$1" | |
| if [[ -e "${project_name}" ]]; then | |
| echo "Error: '${project_name}' already exists." >&2 | |
| exit 1 | |
| fi | |
| mkdir -p "${project_name}/src" | |
| cat > "${project_name}/src/${project_name}.c" <<EOF | |
| #include <stdio.h> | |
| int main(void) | |
| { | |
| printf("printonscreen!\n"); | |
| return 0; | |
| } | |
| EOF | |
| cat > "${project_name}/.clang-format" <<EOF | |
| BasedOnStyle: LLVM | |
| IndentWidth: 8 | |
| TabWidth: 8 | |
| UseTab: Always | |
| BreakBeforeBraces: Linux | |
| AllowShortIfStatementsOnASingleLine: false | |
| IndentCaseLabels: false | |
| EOF | |
| cat <<EOF > "${project_name}/Makefile" | |
| CC ?= gcc | |
| TARGET = bin/${project_name} | |
| SOURCE = src/${project_name}.c | |
| CFLAGS = -Wall -Wextra -O2 -g | |
| .PHONY: all | |
| all: \$(TARGET) | |
| .PHONY: help | |
| help: ## Display this help. | |
| @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", \$\$1, \$\$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr(\$\$0, 5) }' \$(MAKEFILE_LIST) | |
| \$(TARGET): \$(SOURCE) | |
| @mkdir -p \$(dir \$@) | |
| @echo "Compiling \$< -> \$@" | |
| @\$(CC) \$(CFLAGS) -o \$@ \$< | |
| .PHONY: clean | |
| clean: ## Remove build artifacts | |
| @echo "Cleaning" | |
| @rm -rf bin | |
| EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment