Andrew Rogers
Andrew Rogers

Reputation: 106

Add Eigen to android

Hello I'm having some issues trying to include Eigen into my android ndk project.

This is the error message I get when trying to run the application:

Build command failed.
Error while executing process C:\Users\Andrew\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\cmake.exe with arguments {--build D:\newnativetest\app\.externalNativeBuild\cmake\debug\arm64-v8a --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o
FAILED: CMakeFiles/native-lib.dir/native-lib.cpp.o 
C:\Users\Andrew\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe -target aarch64-none-linux-android -gcc-toolchain C:/Users/Andrew/AppData/Local/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/Andrew/AppData/Local/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64  -Dnative_lib_EXPORTS -ID:/newnativetest/app/src/main/cpp/Eigen -isystem C:/Users/Andrew/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem C:/Users/Andrew/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/include -isystem C:/Users/Andrew/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -std=c++11 -O0 -fno-limit-debug-info  -fPIC -MD -MT CMakeFiles/native-lib.dir/native-lib.cpp.o -MF CMakeFiles\native-lib.dir\native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/native-lib.cpp.o -c D:/newnativetest/app/src/main/cpp/native-lib.cpp
In file included from D:/newnativetest/app/src/main/cpp/native-lib.cpp:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Core:458:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/Matrix.h:14:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/../../Eigen:1:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Dense:2:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/LU:28:
D:/newnativetest/app/src/main/cpp/Eigen/src/LU/FullPivLU.h:255:24: error: no member named 'rcond_estimate_helper' in namespace 'Eigen::internal'
      return internal::rcond_estimate_helper(m_l1_norm, *this);
             ~~~~~~~~~~^
In file included from D:/newnativetest/app/src/main/cpp/native-lib.cpp:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Core:458:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/Matrix.h:14:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/../../Eigen:1:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Dense:2:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/LU:29:
D:/newnativetest/app/src/main/cpp/Eigen/src/LU/PartialPivLU.h:187:24: error: no member named 'rcond_estimate_helper' in namespace 'Eigen::internal'
      return internal::rcond_estimate_helper(m_l1_norm, *this);
             ~~~~~~~~~~^
In file included from D:/newnativetest/app/src/main/cpp/native-lib.cpp:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Core:458:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/Matrix.h:14:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/../../Eigen:1:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Dense:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Cholesky:32:
D:/newnativetest/app/src/main/cpp/Eigen/src/Cholesky/LLT.h:165:24: error: no member named 'rcond_estimate_helper' in namespace 'Eigen::internal'
      return internal::rcond_estimate_helper(m_l1_norm, *this);
             ~~~~~~~~~~^
D:/newnativetest/app/src/main/cpp/Eigen/src/Cholesky/LLT.h:535:36: error: out-of-line definition of 'llt' from class 'SelfAdjointView<MatrixType, Mode>' without definition
SelfAdjointView<MatrixType, UpLo>::llt() const
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
D:/newnativetest/app/src/main/cpp/Eigen/src/Cholesky/LLT.h:537:14: error: use of undeclared identifier 'PlainObject'
  return LLT<PlainObject,UpLo>(m_matrix);
             ^
In file included from D:/newnativetest/app/src/main/cpp/native-lib.cpp:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Core:458:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/Matrix.h:14:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/../../Eigen:1:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Dense:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Cholesky:33:
D:/newnativetest/app/src/main/cpp/Eigen/src/Cholesky/LDLT.h:220:24: error: no member named 'rcond_estimate_helper' in namespace 'Eigen::internal'
      return internal::rcond_estimate_helper(m_l1_norm, *this);
             ~~~~~~~~~~^
D:/newnativetest/app/src/main/cpp/Eigen/src/Cholesky/LDLT.h:654:36: error: out-of-line definition of 'ldlt' from class 'SelfAdjointView<MatrixType, Mode>' without definition
SelfAdjointView<MatrixType, UpLo>::ldlt() const
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
D:/newnativetest/app/src/main/cpp/Eigen/src/Cholesky/LDLT.h:656:15: error: use of undeclared identifier 'PlainObject'
  return LDLT<PlainObject,UpLo>(m_matrix);
              ^
In file included from D:/newnativetest/app/src/main/cpp/native-lib.cpp:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Core:365:
D:/newnativetest/app/src/main/cpp/Eigen/src/Core/util/ForwardDeclarations.h:121:63: error: implicit instantiation of undefined template 'Eigen::Array<long, 1, -1, 1, 1, -1>'
         typename StrideType = typename internal::conditional<PlainObjectType::IsVectorAtCompileTime,InnerStride<1>,OuterStride<> >::type > class Ref;
                                                              ^
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:101:11: note: in instantiation of default argument for 'Ref<Eigen::Array<long, 1, -1, 1, 1, -1>, 0>' required here
  typedef Ref<ArrayXi> IndicesRef;
          ^~~~~~~~~~~~
D:/newnativetest/app/src/main/cpp/Eigen/src/Core/util/ForwardDeclarations.h:242:60: note: template is declared here
         int _MaxRows = _Rows, int _MaxCols = _Cols> class Array;
                                                           ^
In file included from D:/newnativetest/app/src/main/cpp/native-lib.cpp:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Core:458:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/Matrix.h:14:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/../../Eigen:1:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Dense:5:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/SVD:38:
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:193:11: error: implicit instantiation of undefined template 'Eigen::Array<long, 1, -1, 1, 1, -1>'
  ArrayXi m_workspaceI;
          ^
D:/newnativetest/app/src/main/cpp/Eigen/src/Core/util/ForwardDeclarations.h:242:60: note: template is declared here
         int _MaxRows = _Rows, int _MaxCols = _Cols> class Array;
                                                           ^
In file included from D:/newnativetest/app/src/main/cpp/native-lib.cpp:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Core:458:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/Matrix.h:14:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/../../Eigen:1:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Dense:5:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/SVD:38:
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:599:16: error: implicit instantiation of undefined template 'Eigen::Map<Eigen::Array<long, 1, -1, 1, 1, -1>, 0, Eigen::Stride<0, 0> >'
  Map<ArrayXi> perm(m_workspaceI.data(),m);
               ^
D:/newnativetest/app/src/main/cpp/Eigen/src/Core/util/ForwardDeclarations.h:118:99: note: template is declared here
template<typename MatrixType, int MapOptions=Unaligned, typename StrideType = Stride<0,0> > class Map;
                                                                                                  ^
In file included from D:/newnativetest/app/src/main/cpp/native-lib.cpp:3:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Core:458:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/Matrix.h:14:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/../../Eigen:1:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/Dense:5:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/SVD:38:
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:694:17: error: member reference base type 'const IndicesRef' (aka 'const int') is not a structure or union
  Index m = perm.size();
            ~~~~^~~~~
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:698:19: error: called object type 'IndicesRef' (aka 'int') is not a function or function pointer
    Index j = perm(i);
              ~~~~^
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:901:17: error: member reference base type 'const IndicesRef' (aka 'const int') is not a structure or union
  Index m = perm.size();
            ~~~~^~~~~
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:907:20: error: called object type 'IndicesRef' (aka 'int') is not a function or function pointer
  Index last = perm(m-1);
               ~~~~^
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:921:23: error: called object type 'IndicesRef' (aka 'int') is not a function or function pointer
        Index i = perm(l);
                  ~~~~^
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:924:35: error: called object type 'IndicesRef' (aka 'int') is not a function or function pointer
          Index j = i<k ? i : perm(l-1);
                              ~~~~^
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:949:17: error: member reference base type 'const IndicesRef' (aka 'const int') is not a structure or union
  Index m = perm.size();
            ~~~~^~~~~
D:/newnativetest/app/src/main/cpp/Eigen/src/SVD/BDCSVD.h:963:23: error: called object type 'IndicesRef' (aka 'int') is not a function or function pointer
        Index i = perm(l);
                  ~~~~^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
ninja: build stopped: subcommand failed.



This is my first time trying to include an external library into my android project so this issue may be obvious But I tried following the answer to this question.

I will attatch below the files that I think are relevant, if there is anything else you need please let me know.

CMakeLists.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

include_directories(src/main/cpp/Eigen)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

Build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.gpcsl.myapplication"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Native-lib.cpp

#include <jni.h>
#include <string>
#include "Eigen/Core"

extern "C" JNIEXPORT jstring JNICALL
Java_com_gpcsl_myapplication_MainActivity_stringFromJNI(JNIEnv *env,jobject /* this */)
{
    Eigen::Matrix2d b;


    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

I've also noticed that If I actually open any of the Eigen src files that there is a lot of errors for example:Example

Upvotes: 3

Views: 878

Answers (1)

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

Do not include anything under the Eigen/src/ directory. That is essentially the internal implementation. Instead of #include "Eigen/src/Core/Matrix.h" you want to include #include "Eigen/Core" or #include "Eigen/Dense".

Based on chtz's comment, the actual problem was that the internal implementation was modified by OP, which made the internal implementation include the exposed API. This was noticeable from the compiler errors:

In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/Matrix.h:14:
In file included from D:/newnativetest/app/src/main/cpp/Eigen/src/Core/../../Eigen:1:

Upvotes: 2

Related Questions