CMake
CMake Installation
On Windows:
- Make sure you have installed scoop first.
- Run
scoop install cmake
in PowerShell.
On macOS:
- Make sure you have installed
homebrew
first. - Run
brew install cmake
in terminal.
Linux users can install CMake using their package manager.
clangd Installation
Windows:
- Run
scoop install clangd
in PowerShell.
macOS:
- Run
brew install llvm
in terminal.
Ubuntu:
- Try
sudo apt-get install clangd-12
. If that’s not found, trysudo apt-get install clangd-9
.
Why CMake?
- CMake is a cross-platform build system generator.
- It generates native build files for various compilers.
- It is widely used in the industry.
- It is easier to include third-party libraries in your project.
A simple example
Create a file named CMakeLists.txt
in your project’s root directory.
A basic CMakeLists.txt
consists of three lines:
cmake_minimum_required(VERSION 3.10)
project(MyProject)
add_executable(my_program main.cpp)
cmake_minimum_required(VERSION 3.26)
: specifies the minimum version of CMake required to build the project.project(MyProject)
: defines the name of the project.add_executable(main main.cpp)
: tells CMake to build an executable namedmain
from the source filemain.cpp
.
Project with multiple source files
If you have multiple source files, you can add them to the add_executable
command.
cmake_minimum_required(VERSION 3.10)
project(MyProject)
add_executable(my_program main.cpp helper.cpp)
In this lab we don’t discuss how to build a project with subdirectories and multiple targets. All the headers and source files are in the same directory.
C++ standard
We can specify the C++ standard in CMake.
set
is used to set a variable.CMAKE_CXX_STANDARD
is used to set the C++ standard.CMAKE_CXX_STANDARD_REQUIRED
is used to specify that the specified C++ standard is required.
cmake_minimum_required(VERSION 3.10)
project(MyProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(my_program main.cpp helper.cpp)
Build the project
In the terminal, navigate to the root directory of your project and run:
cmake -B build -S .
cmake --build build
- The space between
-B
andbuild
can be omitted, the same for-S
.-B
specifies the build directory.-S
specifies the source directory.
- The first command generates build files in the
build
directory. - The second command calls the build files to build the project.
Generate project files for other IDEs
What are the build tools on your system?
If you type cmake -G
in terminal, you can see the options to generate build files available on your system.
However, if you want to generate project files for Ninja1, you can type:
cmake -G Ninja -B build -S .
cmake --build build
When to run cmake -B build -S .
:
- First-time project setup: Run this command when initially setting up the project.
- Regenerating build files: Use this command if you need to recreate the build files.
- Modifying CMakeLists.txt: Run this after adding or removing source files in your CMakeLists.txt.
When to run cmake --build build
:
- After running
cmake -B build -S .
, use this command to build your project. - You can use this command for subsequent builds as long as you haven’t modified CMakeLists.txt or need to regenerate build files.
Suggested IDEs
- CLion: built-in CMake support.
- Visual Studio Code: with CMake extension.
- Visual Studio: with Visual Studio IDE.
Assignment
Configure an IDE of your choice to build and finish the assignment. The function prototypes are given and you need to implement them.
- Finish helper functions:
printMatrix
,printVector
. - Finish functions:
dotProduct
,outerProduct
,matrixMultiplication
. - Perform input validation by checking matrix and vector sizes before operations.
- If sizes are invalid, output an error message and return an empty matrix/vector. For
dotProduct
, return 0.
- If sizes are invalid, output an error message and return an empty matrix/vector. For
Hints & submission
- You can use
std::cout
to print error messages. - If you need to refresh your understanding of matrix/vector operations, consult these Wikipedia pages:
- dot product
- outer product
- matrix multiplication
- Only submit your source files(
.cpp
), header files(.h
) andCMakeLists.txt
. Zip them up and submit to Canvas.
Footnotes
ninja is a build tool similar to make, but faster, it’s commonly used on Unix-like systems↩︎