Add Eigen Library to Android NDK
Asked Answered
C

2

8

I need to include Eigen library in my Android Studio project to do some linear algebra operations and use some C++ code that I've developed for desktop. I've been looking for information on this topic, but there isn't too much and I'm new on Android NDK. Eigen library doesn't need to be compiled, so I thought It would be easy, but I'm missing something. I've tried to copy the Eigen folder (which have all the includes) into the NDK folder (..\Android\Sdk\ndk-bundle\sysroot\usr\include) where there are other exteral libraries (GLES for example) but Android Studio don't detect it. How can I do it?? Thank you in advance, I really need this.

Edit 1: As you can see here, Eigen lib is included, but when I compile the project there are many errors, and I don't know why. Any ideas?

Character answered 18/1, 2018 at 9:22 Comment(10)
See this and thisPalisade
Thank you very much. I added this line include_directories(src/main/cpp/Eigen/)Now to my CMakeLists.txt and now Visual Studio "sees" the Eigen include files, but the project don't compile (see edit).Character
The compiler cannot "see" your header files, which means it is trying to compile your c++ source code. (I think you will need to compile from source because you will need multiple versions armeabi, armeabi-v7a, arm64-v8a etc... for mutliple possible processors)Palisade
But Eigen don't need to be compiled, is a library with header files only, or this is what I've thought. How can I compile it in Android?Character
If you want a native library as output (multiple versions of .so files) you need to compile the headers (even though they are .h header files, they have active c++ code that needs to be compiled, just as if you had .cpp files). Your getting things like undeclared identifier Matrix, so the compiler cannot see the definition of Matrix for example.Palisade
Ok I get it. But How can I compile this library? I have to include it in the CMakeLists.txt of my project?Character
Before we do that, have you given any consideration to how to call the library functions/methods from Java (using JNI), or are you making an ALL C++ (no Java AT ALL) android app ? I have the feeling we will compile the library and then you will not have any way of using it ? Maybe you are better suited to an "Android flavoured C++ Eigen library", or a Java version of "Eigen library".Palisade
See this #8543721Palisade
My application has the GUI in Java, but I do data processing (expensive math operations with machine learning algorithms) using C++ and return the processed data to Java side with JNI because it improves the performance. To implement these algorithms I use this library, which is in C++ and really simplifies linear algebra operations. So My plan is to use this library in C++ including its headers in Android NDK, not calling it from Java side.Character
That link could be really useful, I dind't see it before!. I'm going to read it carefully, thank you!Character
C
7

Finally I reach my objective, and I'm Working with Eigen in Android. If you are triying to use Eigen library in Android, I hope this help you:

  1. Download Eigen library from the official site.
  2. Copy Eigen folder inside the zip you have downloaded in which are all the headers (.h files) of the library and paste it on one folder of your choice in the project. For example, i did it in:

    ../app/src/main/cpp

  3. Edit CMakeLists.txt, adding this line with the path of the Eigen folder inside your project: include_directories(src/main/cpp/Eigen)
  4. Launch the App in a real device (not working on emulator) to load the Eigen header files in it.
  5. Include in your cpp file the Eigen headers and work with them normally. For example:

    #include "Eigen/Dense" void multiply2Matrices(){ Eigen::MatrixXd M(2,2); Eigen::MatrixXd V(2,2); for (int i = 0; i<=1; i++){ for (int j = 0; j<=1; j++){ M(i,j) = 1; V(i,j) = 2; } } Eigen::MatrixXd Result = M*V; }

In my case, I didn't have to compile anything, jus use the header files of official Eigen library

Character answered 18/1, 2018 at 16:13 Comment(1)
I download Eigen source files from eigen.tuxfamily.org. I put eigen folder at same folder as CMakeLists.txt file. And adding include_directories(eigen) in CMakeLists.txt file.Stradivarius
W
0
  1. Clone Eigen git repo from https://github.com/eigenteam/eigen-git-mirror for example to ThirdParty/eigen

  2. Simply add Eigen to build process, e.g. to your root CMakeLists.txt

    add_subdirectory(ThirdParty/eigen)

  3. And use it:

    add_library(${PROJECT_NAME} SHARED src/test_eigen.cpp)

    target_link_libraries(${PROJECT_NAME} Eigen3::Eigen)

So, nothing special in usage for Android NDK, just include Eigen stuff to native build. Some issues which happened during my test that I had to switch off some Eigen stuff, for example doc target compilation in ThirdParty/eigen/CMakeLists.txt because it contradicted with the already existed target in my project:

if(NOT ANDROID)
    add_subdirectory(doc EXCLUDE_FROM_ALL)
endif()

Full example usage can be found here: https://github.com/nkh-lab/ndk-eigen-hello-world

Woodwind answered 8/7, 2020 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.