A minimal CMake learning tool and template for C++ projects.
- CMake (version 3.5 or newer) must be installed and available in your PATH.
- A C++ compiler must be installed and available in your PATH:
- For C++ Compilers: Visit This Link
If you use this repository as a template, read this description!
- The
srcandutilsfolders are for organizing your.cppfiles. - Build directories (e.g.,
bin_native) are for your compiled output. - Build directories must contain the string "bin" in their name to be excluded from source file discovery by this project's
CMakeLists.txt. This is a specific setup for this repository, not a universal CMake rule.
.
├── src/ # Source files (e.g., calc.cpp)
├── utils/ # Utility source files (e.g., nPow.cpp)
│
├── bin_native/ # Default build output directory for native compilation
│
├── CMakeLists.txt # Main CMake configuration file (required)
│
└── README.md # This file
This is the standard way to build the project using the native g++ compiler that you likely already have on your system. This will compile the project for your current operating system and architecture. bin_native is just an example name for the build directory used in this repository.
Open a terminal in the project root and run:
cmake -S . -B bin_native -DCMAKE_CXX_COMPILER=g++-S .: Source directory (whereCMakeLists.txtis located)-B bin_native: Build directory (where build files and cache will be generated)
After configuration, build the project with:
cmake --build bin_native- This uses the cached settings in
bin_nativefrom the previous step. - The resulting executable (e.g.,
apporapp.exe) will be placed in thebin_nativedirectory.
From the project root, the execution command depends on your operating system and shell:
On Linux/macOS (Bash/Zsh):
./bin_native/appOr, if you need to pass arguments:
./bin_native/app <base> <exponent>On Windows (PowerShell):
.\bin_native\app.exeOr, if you need to pass arguments:
.\bin_native\app.exe <base> <exponent>On Windows (Command Prompt):
bin_native\app.exeOr, if you need to pass arguments:
bin_native\app.exe <base> <exponent>Cross-compilation is the process of building an executable that will run on a different operating system or architecture than the one you are building on.
To build for aarch64 Linux from an x86_64 Linux machine, you will need the aarch64-linux-gnu-g++ cross-compiler.
cmake -S . -B binL_aarch64 -G "Unix Makefiles" -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++
cmake --build binL_aarch64To build for x86_64 Windows from a Linux machine, you will need the MinGW-w64 toolchain, specifically the x86_64-w64-mingw32-g++ compiler.
cmake -S . -B binW_x86_64 -G "Unix Makefiles" -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++
cmake --build binW_x86_64Note: The toolchain for Windows on ARM64 is experimental and not yet available in official repositories. You will likely need to build it from source. A community effort to create this toolchain can be found at Windows-on-ARM-Experiments/mingw-woarm64-build.
Once the aarch64-w64-mingw32-g++ compiler is available on your system, you can use the following command:
cmake -S . -B binW_aarch64 -G "Unix Makefiles" -DCMAKE_CXX_COMPILER=aarch64-w64-mingw32-g++
cmake --build binW_aarch64- You can change the build directory name (e.g.,
bin_native,binL_aarch64, etc.), but remember that it must contain the string "bin" due to this project'sCMakeLists.txtconfiguration. - To clean the build, simply delete the corresponding build directory (e.g.,
rm -rf bin_native). - If you change the generator or toolchain, always delete the build directory and re-run the configuration step.
- NixOS Users: If you are using NixOS, you might need to specify the
--sysrootflag to point to the correct system root when runningcmake.
-
Configure:
cmake -S <source_dir> -B <build_dir> -G <build_tool> -DCMAKE_CXX_COMPILER=<compiler>-G <build_tool>: if not set by default CMake uses NMake (on Windows) or Unix Makefiles (on Linux/macOS).- Use
-G "MinGW Makefiles"if you don't want to use NMake and prefer the defaultmingw32-makegenerator that comes with your MinGW compiler.
-
Build:
cmake --build <build_dir> -
Clean:
cmake --build <build_dir> --target clean -
Verbose Build:
cmake --build <build_dir> --verbose
Happy Coding! 🚀