Getting Started with Eigen Library in C++

Eigen is a C++ template library designed for linear algebra, offering functionalities for matrix and vector operations, numerical solvers, and related algorithms. In this guide, I will explain how to set up the Eigen library for Visual Studio on Windows and Visual Studio Code on Linux (Ubuntu).

The Eigen library can be downloaded from [1]. The website provides the latest version, detailed documentation, and a list of projects that utilize the library. From my experience, Eigen is one of the best-in-class C++ libraries for linear algebra.

For this post I reference information from Aleksander Haber [2],[3], and from other resources from stack exchange.

Installation Steps for Windows:

To use Eigein, it is recommended to install Visual Studio Code as described in [3]. Here’s a quick overview of the process:

  • Install Microsoft Visual Studio Code.
  • During installation, select “desktop development with C++”. Ensure to include the optional subpackage “MSVC v143 - VS 2022 C++ x64/x86 build tools”.
  • Use the terminal “Developer Command Prompt for VS 2022”.

Folder Setup:

For convenience, create a folder in your C: drive named “codes”. Inside this folder, create subfolders for each project and a folder for toolboxes. Store the Eigen library in the “toolboxes” folder.

To navigate via terminal:

  • Use “cd ..” to move up a directory.
  • Once in the C: drive, use “cd codes”.
  • To display all directories, use dir.
  • Create a folder using mkdir testfolder.
  • Launch Visual Studio Code using code . from the “Developer Command prompt for VS 2022” terminal.

Adding Eigen Library to the Project

  • After creating a “HelloWorld” program in VS Code, a “tasks.json” will be generated under “.vscode”.
  • Add the following line to include the Eigen library path: “-IC:/codes/Toolbox/eigen-3.4.0/”, as in code 1 below.

            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                "-IC:/codes/Toolbox/eigen-3.4.0/"

Code 1: “args” in the tasks.json file.


  • Next, open the “C/C++: Edit configurations (UI)” using “ctrl + shift + P”. This will generate a “c_cpp_properties.json” file under “.vscode”.
  • Add the following line: “C:/codes/Toolbox/eigen-3.4.0/**” as in code 2 below and remember to include the comma.

            "includePath": [
                "${workspaceFolder}/**",
                "C:/codes/Toolbox/eigen-3.4.0/**"
            ],

Code 2: “includePath” in the c_cpp_properties.json file.


Sample Code

  • Below, in code 3, is a simple program using the Eigen library.

#include <iostream>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

int main()
{

    Matrix <float,3,3> matrixA;

    matrixA.setZero();

    cout << matrixA << endl;

    cout << "Hello" << endl;
    cout << "Hello again" << endl;

    return 0;
}

Code 3: Code that includes the Eigen library.


  • The expected output will be:
0 0 0
0 0 0
0 0 0
Hello
Hello again

Installation Steps for Ubuntu (Linux):

  1. Open Terminal
  2. Run the following commands to install Eigen:
    • sudo apt update
    • sudo apt install libeigen3-dev
  3. Create a symbolic link to make the Eigen library available in the local directory for Visual Studio Code:
    • sudo ln -s /usr/include/eigen3/Eigen /usr/local/include/Eigen

Another way to compile your code and include the Eigen library is by running the following command:

  • g++ -I/usr/include/eigen3 main.cpp algorithms.cpp -o my_program
  • In this case, the -I/usr/include/eigen3 flag specifies the path to the Eigen library.

Reference

[1] Eigen Library Website. Link to reference

[2] Aleksander Haber. YouTube. Link to reference

[3] Aleksander Haber. YouTube. Link to reference

Written on January 1, 2024