Skip to content

Instantly share code, notes, and snippets.

@danielTobon43
Last active June 10, 2021 12:14
Show Gist options
  • Save danielTobon43/8ef3d15f84a43fb15f1f4a49de5fcc75 to your computer and use it in GitHub Desktop.
Save danielTobon43/8ef3d15f84a43fb15f1f4a49de5fcc75 to your computer and use it in GitHub Desktop.
This is a tutorial of how to use Eigen3 on windows 10 and CMake

Eigen3 project

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.

Getting Started

These instructions will get you a full Eigen3 CMake template for your C++ project.

Prerequisites

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.

Note

When you are installing MinGW you must select Architecture: x86_64 on the setup settings.

Setting CMake, MinGW enviroment variables

For this project we are using CMake 3.18 and MinGW 64.

  1. Open the Control Panel.
  2. Click System and Security, then System
  3. Click Advanced system settings on the left
  4. 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

Setting up Eigen3 in your own C++ project

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.

Compiling and running the program

  1. In your build folder run:
$ cmake -G "MinGW Makefiles" ../
$ make
  1. Run executable:
$ ./MatrixMulTr.exe
  1. 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
##############################################################################
# CMAKE CONFIGURATION
##############################################################################
cmake_minimum_required(VERSION 3.5.1 FATAL_ERROR)
# set project name
project(MatrixMulTr VERSION 1.0.0)
# set build type = Debug mode
set(CMAKE_BUILD_TYPE Release)
message("\n" "=========================================")
message("Project: ${PROJECT_NAME} ")
message("=========================================")
# set the include directive in the same project folder
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# set cmake for use std c++11 and output executable folder to bin
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
##############################################################################
# PACKAGES
##############################################################################
message("***********************")
message("EIGEN3 PACKAGE")
message("***********************")
set(EIGEN3_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib/eigen-3.3.9/" )
if(NOT EIGEN3_INCLUDE_DIR )
message(FATAL_ERROR "Please point the variable EIGEN3_INCLUDE_DIR to the include directory of your Eigen3 installation.")
endif()
message(STATUS " Eigen3 directory: ${EIGEN3_INCLUDE_DIR}")
##############################################################################
# HEADERS
##############################################################################
include_directories(${EIGEN3_INCLUDE_DIR})
##############################################################################
# SOURCE CODE
##############################################################################
set(MAIN_SOURCE "src/main.cpp")
##############################################################################
# EXECUTABLES
##############################################################################
add_executable(${PROJECT_NAME} ${MAIN_SOURCE})
message("=========================================")
message("Project: ${PROJECT_NAME} COMPILED WITH CMAKE " ${CMAKE_VERSION})
message("=========================================")
/*********************************
HEADERS
**********************************/
#include <Eigen/Dense>
#include <iostream>
int main(int argc, char **argv) {
std::cout << "\n************************************************" << std::endl;
std::cout << " Matrix Multiplication|Transpose " << std::endl;
std::cout << "************************************************" << std::endl;
Eigen::Matrix<float, 3, 3> matrix1;
matrix1 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
Eigen::Matrix<float, 3, 3> matrix2;
matrix2 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
Eigen::Matrix<float, 3, 3> result;
result = matrix1 * matrix2;
std::cout << "matrix vector multiplication:\n" << result << std::endl;
std::cout << "matrix vector mult transpose:\n" << result.transpose() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment