Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a makefile for a C++ project which uses Eigen, the C++ template library for linear algebra?

I'm making use of Eigen library which promises vectorization of matrix operations. I don't know how to use the files given in Eigen and write a makefile. The source files which make use of Eigen include files as listed below, these are not even header files (They are just some text files)-

<Eigen/Core>
<Eigen/Dense>
<Eigen/Eigen>

and so on. On Eigen's webpage, it's mentioned that, in order to use its functions I don't have to build the project, then how can I include these files in my makefile to build my project. My example main.c file looks like this. Can anyone show me how to write a makefile makefile for this file -

#include <Eigen/Core>

// import most common Eigen types 
USING_PART_OF_NAMESPACE_EIGEN

int main(int, char *[])
{
  Matrix3f m3;
  m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
  Matrix4f m4 = Matrix4f::Identity();
  Vector4i v4(1, 2, 3, 4);

  std::cout << "m3\n" << m3 << "\nm4:\n"
    << m4 << "\nv4:\n" << v4 << std::endl;
}

Help!

like image 414
HaggarTheHorrible Avatar asked Dec 29 '22 10:12

HaggarTheHorrible


1 Answers

According to Eigen's website, this is a header-only library.

This means there is nothing to compile or link it to. Instead, as long as you have the header files in a standard location (/usr/local/include on *nix/Mac), then all you have to do is add that location to your preprocessor build step.

Assuming that you are running *nix/Mac, and assuming that you have everything installed to the default locations (e.g. #include <Eigen/Core> references the file /usr/local/include/Eigen/Core), then a SUPER simple makefile would look like this:

main: main.cpp
    g++ -I /usr/local/include main.cpp -o main

Which says, in English:

  • main depends on main.cpp
  • to make main, use g++ to
    • compile main.cpp,
    • output file main,
    • looking in the directory /usr/local/include for any headers it doesn't know about

NOTE: there is a TAB in front of the g++ line, NOT four spaces.

Hope that helps.

like image 94
Austin Hyde Avatar answered Jan 13 '23 14:01

Austin Hyde