The CMake Project Generator is a Python-based tool that automates the setup of C++ and Qt projects using CMake. It provides a consistent project structure and ready-to-use configuration files, letting you focus on coding instead of boilerplate setup.
- 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
- CMake 3.16+
- A C++17 capable compiler
-For
qtinit: Qt5 or Qt6 development packages (qtbase5-dev on Ubuntu, or Qt SDK installed manually)
- 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.
- For Qt projects: sets up MainWindow.h/.cpp, QApplication, Q_OBJECT macro, and a build system ready for Qt5/Qt6.
- ntegrates with your shell (
add_to_path.py) so scripts can be run 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 cppinitThis script will:
- Copy
cppinitto ~/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_scriptTo 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 .To use the cppinit, simply run the Python script with the project name as an argument:
./qtinit <project_name>
# or
python qtinit <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
./qtinit
# or
python qtinit
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.
./qtinit
Please enter project name:
<project_name>Result Structure
<project_name>/
├── main.cpp
├── .clang-format
├── .gitignore
├── CMakeLists.txt
├── build/
├── include/
│ └── MainWindow.h
└── src/
└── MainWindow.cpp- Generate a project:
cppinit MyProject- Navigate to your project directory:
cd MyProject- Build the project:
cd build
cmake ..
cmake --build .With this setup, you can now generate either plain C++ projects (cppinit) or Qt Widgets projects (qtinit), both ready to build immediately.