Eigen C++ Template Library with C++Builder and VCL

This post describes how to using the Eigen C++ Template Library with C++ Builder and VCL.


What is the Eigen C++ Template Library?
Eigen is a C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms.  A Gentle Introduction to Linear Algebra is here.   Eigen doesn’t have any dependencies other than the C++ standard library.  Eigen consists only of header files, so there is nothing to compile before you can use it.  There is no binary library to link to, and no configured header file. Eigen is a pure template library defined in the headers.

What is Linear algebra?  Linear algebra is the study of lines and planes, vector spaces and mappings that are required for linear transforms. It is a relatively young field of study, having initially been formalized in the 1800s in order to find unknowns in systems of linear equations.

A linear equation is just a series of terms and mathematical operations where some terms are unknown; for example: y = 4 * x + 1

Equations like this are linear in that they describe a line on a two-dimensional graph. The line comes from plugging in different values into the unknown x to find out what the equation or model does to the value of y.

Eigen’s source code: In order to use Eigen, you just need to download and extract Eigen’s source code from here.  The header files in the Eigen subdirectory are the only files required to compile programs using Eigen. The header files are the same for all platforms. It is not necessary to use CMake or install anything!

How to use the Eigen Library with C++ Builder? 

  1. Download and extract Eigen’s source code from here.
  2. Add your Eigen Source Folder to your C++ Builder Include and Library Paths.  In my case, my Eigen Source Folder is: C:/Users/amannarino/Documents/Eigen3/Eigen
  3. In C++ Builder, use Tools | Options | Language | C++ | Paths and Directories

librarypath    include lib

4. In my sample C++ Builder VCL application, in my MainUnit.cpp file, I added the Eigen header files I wanted to use to test, for example, the member functions for the MatrixXd and IOFormat types, to try the Eigen Getting Started example from here: https://eigen.tuxfamily.org/dox/GettingStarted.html


5. This is the first Eigen sample program we will try to build and run using C++ Builder 12:

Let’s create the above sample application in C++ Builder 12:

1. File | New | VCL Application – C++ Builder


2. File | Save Project As, create a new folder, Eigen1. Project File name = Eigen1.cbproj


3. Rename and Save Unit1.cpp to uMain.cpp


4. Since, there is no library to link to, the only thing that you need to keep in mind when compiling the application is that the compiler must be able to find the Eigen header files. (The directory in which you placed Eigen’s source code must be in the include path). In C++ Builder 12, use Project | Options, C++ Shared Options, Include path, and add the directory where you placed Eigen’s source code. In my case it’s folder C:UsersamannarinoDocumentsEigen3



5. And also include the Eigen files to the C++ Builder Project Library Path (Project | Options, C++ Shared Options, Library path) 


6. Now that the Include path has the location of the Eigen header files, we can include the Eigen/Dense, in the uMain.cpp like this:

and we can use the Eigen Matrix class (Dense matrix and array manipulation) described here: https://eigen.tuxfamily.org/dox/group__DenseMatrixManipulation__chapter.html

and use the Eigen IOFormat Class described here: https://eigen.tuxfamily.org/dox/structEigen_1_1IOFormat.html

by adding this code:


7. To verify the C++ Builder application can find the needed Eigen file, BUILD the application.
If you get the error: [bcc32c Fatal Error] uMain.cpp(6): ‘Eigen/Dense’ file not found
then you have the incorrect Library path for the Eigen source code.

8. The C++ Builder 12 source code for this application adapted from Eigen documentation for our VCL application for our first Eigen sample program is this code in our C++ Builder uMain.cpp file:

9.Build and Run the application.  Click on the Using Eigen/Dense button, and you will see the Matrix Output, like this:

eigen denseapp

Congratulations!  You now know how to using the Eigen C++ Template Library with C++ Builder and VCL.

If you are interetsed in learning more on what you can do with the Eigen C++ Template Library with C++ Builder and VCL, it’s worth taking the time to read this long tutorial on The Matrix class (Dense matrix and array manipulation).

You can try using the Eigen C++ Template Library with C++ Builder and VCL by downloading and installing the free 30 day trial of the current C++ Builder 12 from here!

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition