0

I'm trying to use Eigen3 to calculate inversion of a matrix, but during compile it throws an error

my Eigen3 vision is 3.3.9, run under Ubuntu 18.04

CMakeFiles/test.dir/test.cpp.o:in function‘main’:
test.cpp:(.text+0x2a7):undefined reference of‘Eigen::MatrixBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> >::inverse() const’

(this might not be the exact error info because I translate it from Chinese, but I can ensure it says "undefined reference")

and this is my code

#include<Eigen/Core>
#include<iostream>

using namespace std;
using namespace Eigen;
int main()
{
    Matrix4f M;

M<< 0.932322,  0.125848,  -0.85106, -0.313612,  -1.50979, -0.691102,
0.125848,  0.663803, -0.555993,  0.117918, -0.645694, -0.625737,
-0.85106, -0.555993,   1.26442,   0.39079;

cout << "Inverse= " << M.inverse() << endl;
}

and my CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(test)

find_package(Eigen3 REQUIRED)

include_directories(${EIGEN3_INCLUDE_DIRS})

add_executable(test test.cpp)

I noticed similar question but that one is caused by cuda. I do have cuda installed on my computer but I'm not using it.

Thank you for reading this and helping me.

catmulti7
  • 19
  • 2
  • You need to link Eigen3 libraries - https://cmake.org/cmake/help/latest/command/target_link_libraries.html – kiner_shah Nov 15 '21 at 12:59
  • Also see this answer: https://stackoverflow.com/a/52230189 – kiner_shah Nov 15 '21 at 13:01
  • 1
    @kiner_shah Thank you for your reply. I tried but it doesn't work, same error again. And I noticed I can use other member function of Matrix normally like trace() or transpose(), it seems only inverse() doesn't work – catmulti7 Nov 15 '21 at 13:37
  • Looks like multiple Eigen versions installed on your machine. Probably included header from one version and linked against the other. – pptaszni Nov 15 '21 at 14:00
  • A random choice : Add "Eigen header" → A new line with `#include` .... then no issues. ..... ( "Eigen" is a 2line header, just adding two other headers.) – Knud Larsen Nov 15 '21 at 15:22
  • @KnudLarsen this do fix my problem, thank you a lot~ – catmulti7 Nov 16 '21 at 14:27

1 Answers1

3

This may look like a linking error, since the function is forward declared. But Eigen is header-only, so you just need to include the correct header for that:

#include <Eigen/LU>

As documented here: https://eigen.tuxfamily.org/dox/classEigen_1_1MatrixBase.html#a7712eb69e8ea3c8f7b8da1c44dbdeebf

Alternatively, you can include <Eigen/Dense> or <Eigen/Eigen> which both include <Eigen/LU> indirectly.

chtz
  • 17,329
  • 4
  • 26
  • 56