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.
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:
- Download Eigen library from the official site.
- 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
- Edit CMakeLists.txt, adding this line with the path of the Eigen folder
inside your project:
include_directories(src/main/cpp/Eigen)
- Launch the App in a real device (not working on emulator) to load the Eigen header files in it.
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
Clone Eigen git repo from https://github.com/eigenteam/eigen-git-mirror for example to ThirdParty/eigen
Simply add Eigen to build process, e.g. to your root CMakeLists.txt
add_subdirectory(ThirdParty/eigen)
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
© 2022 - 2024 — McMap. All rights reserved.
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