Eigen is a C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms. This tutorial will get you through the steps of how to use Eigen in your own CMake project on Windows 10.
These instructions will get you a full Eigen3 CMake template for your C++ project.
Eigen doesn't have any dependencies other than the C++ standard library. It use the CMake build system, but only to build the documentation and unit-tests, and to automate installation. If you just want to use Eigen, you can use the header files right away. There is no binary library to link to, and no configured header file. Eigen is a pure template library defined in the headers.
- Eigen3: Eigen3
- MinGW: MinGW64 MingW-W64-builds
- CMake: CMake
When you are installing MinGW you must select Architecture: x86_64
on the setup settings.
For this project we are using CMake 3.18 and MinGW 64.
- Open the Control Panel.
- Click System and Security, then System
- Click Advanced system settings on the left
- Inside the System Properties window, click the Environment Variables… button and add the following paths:
CMake: C:\cmake-3.18.0-rc2-win64-x64\bin
MinGW64: C:\MinGW64\mingw64\bin
Reference: editing-system-environment-variables
In this tutorial we are going to use Eigen3 to define 2 by 3x3 matrix to calculate the matrix vector multiplication and its transpose. For this example, my project structure is the following:
- EigenProject:
-- build:
-- lib:
--- eigen-3.3.9:
---- Eigen:
----- headers...
-- src:
--- main.cpp
CMakeLists.txt
When you download eigen3, the only important folder is the Eigen folder
. The other files are not important, so you can delete them.
- In your build folder run:
$ cmake -G "MinGW Makefiles" ../
$ make
- Run executable:
$ ./MatrixMulTr.exe
- Expected output:
************************************************
Matrix Multiplication|Transpose
************************************************
matrix vector multiplication:
30 36 42
66 81 96
102 126 150
matrix vector mult transpose:
30 66 102
36 81 126
42 96 150