A very simple bash-script, generating a full-on directory in compliance with the 42-CPP projects (cuz I'm lazy).
This script streamlines the creation of the 42 C++ projects, exercises, source files, and a Makefile
with customizable options. It's perfect for setting up a structured project environment with minimal effort.
- 📁 Project Directory Creation: Optionally create a project folder and navigate into it.
- 📂 Exercise Directory Creation: Organize exercises in separate directories.
- 📝 Source File and Header File Setup: Automatically generate
.cpp
and optional.hpp
files for your sources. - 🔨
Makefile
Generation: Create aMakefile
with:- Compilation rules.
- Support for
main.cpp
boilerplate. - Optional Valgrind support.
- Clean and rebuild commands.
- 👋 Boilerplate
main.cpp
: Generate amain.cpp
file with a simple "Hello, World!" example.
- Run the Script: Execute the script in your terminal.
- Follow Prompts:
- Decide whether to create a project or exercise directory.
- Provide the project/exercise name.
- Specify source file names and whether to generate corresponding headers.
- Optionally generate a
main.cpp
boilerplate. - Include Valgrind support if needed.
- Files Generated:
.cpp
and.hpp
files as per your input.- A comprehensive
Makefile
tailored to your needs. - Optional
main.cpp
with a basic example.
- A Unix-based shell (Linux or macOS).
bash
installed.
- Save the script to a file, e.g.,
setup_project.sh
. - Make it executable:
chmod +x setup_project.sh
- Run the script:
./setup_project.sh
- Optionally, set an alias for it in your
.*rc
file.
# ~/.bashrc
alias 42cppgen path/to/the/script
- Project name:
MyProject
- Exercise:
ex00
- Source files:
file1.cpp file2.cpp
- Generate headers: Yes
- Add
main.cpp
: Yes - Include Valgrind support: Yes
MyProject/
└── ex00/
├── file1.cpp
├── file1.hpp
├── file2.cpp
├── file2.hpp
├── main.cpp
└── Makefile
# Program
NAME := MyProgram
# Necessities
CXX := c++
CXXFLAGS := -Wall -Wextra -Werror -std=c++98
# Targets
SRC := file1.cpp file2.cpp main.cpp
INCLUDES := $(SRC:.cpp=.hpp)
# Rules
all: $(NAME)
$(NAME): $(SRC)
$(CXX) -o $@ $(CXXFLAGS) $(SRC)
@printf "\e[92;5;118mCompilation successful!\e[0m\n"
clean:
rm -rf $(NAME)
@printf "\e[93;5;226mExecutable removed.\e[0m\n"
valgrind:
@printf "\e[33;3mRunning valgrind...\e[0m\n"
valgrind --leak-check=full ./$(NAME)
re: clean all
.PHONY: all clean re
#include <iostream>
int main(void) {
std::cout << "Hello, world!" << std::endl;
return 0;
}
- This script is interactive, prompting you for inputs at each step.
- It’s highly customizable for your specific project needs.
- Use this tool to focus on coding, not setup!