The CMake Project Generator is a Python script that automates the setup of C++ projects with CMake, providing a consistent project structure and basic configuration files.
- cppinit: Generates the necessary files and directory structure for a new C++ project.
- add_to_path.py: Copies the CMakeProjectGenerator.py script to a ~/scripts directory and updates your PATH environment variable to include this directory, allowing you to run the script from anywhere.
- Python 3.x
- Generates CMakeLists.txt for the project root and test directories.
- reates a main.cpp file, a header file, and a source file.
- Configures .gitignore and .clang-format files for Git and code formatting.
- Integrates with your system for easy access from anywhere.
The add_to_path.py
script copies a specified script (for example cppinit) to the ~/scripts
directory and adds this directory to your PATH environment variable, so you can execute the script from anywhere in the terminal.
If this is the first run, the add_to_path.py
script will also copy itself to the ~/scripts
directory, removing the .py extension.
if is first run
- Ensure both scripts are in the same directory.
- Run the add_to_path.py script with the name of the script you want to copy:
./add_to_path.py cppinit
# or
python add_to_path.py cppinit
This script will:
- Copy
cppinit
to ~/scripts. - Check if ~/scripts is already in your PATH and add it to your .bashrc or .zshrc file if it is not.
- Instruct you to source your shell configuration file to update the PATH in the current session.
cppinit <project_name>
After the first run, the add_to_path.py
script is copied to the ~/scripts directory without the .py extension, allowing you to call it from anywhere:
add_to_path <script_name>
For example, to copy another script from any directory named new_script
to the ~/scripts directory, so you can use it from anywhere:
add_to_path new_script
To use the cppinit, simply run the Python script with the project name as an argument:
./cppinit <project_name>
# or
python cppinit <project_name>
or If you run the script without providing a project name as an argument, it will prompt you to enter the project name
./cppinit
# or
python cppinit
You will then be prompted to enter the project name manually. After providing the project name, the script will proceed to create the project directory and files as usual.
./cppinit
Please enter project name:
<project_name>
Result Structure
<project_name>/
├── main.cpp
│
├── .clang-format
│
├── .gitignore
│
├── CMakeLists.txt
│
├── build/
│
├── include/
│ └── <project_name>.h
│
├── src/
│ └── <project_name>.cpp
│
└── tests/
├── <project_name>_test.cpp
└── CMakeLists.txt
- Generate a project:
cppinit MyProject
- Navigate to your project directory:
cd MyProject
- Build the project:
cd build
cmake ..
cmake --build .