Search code examples
androidandroid-studiolinker-errorsfftw

Linking FFTW3 library to native Android Studio project


I want to link the static fftw3 library, which I built from source, to my native Android Studio application. Therefore I included the headers and added libfftw3.a (Mach-O 64-bit object arm64) to my jniLibs/arm64-v8a/ folder. I link the library with CMake:

set(VENDOR_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/arm64-v8a)

add_library(fftw STATIC IMPORTED)
set_target_properties( fftw PROPERTIES IMPORTED_LOCATION ${VENDOR_LIB_DIR}/libfftw3.a )
target_link_libraries( exec fftw )

In my build.gradle I added the following C++ flags:

defaultConfig {
    applicationId "com.example.demo"
    minSdk 33
    targetSdk 33
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
        cmake {
            arguments "-DCMAKE_TOOLCHAIN_FILE=/Users/username/Library/Android/sdk/ndk/25.2.9519653/build/cmake/android.toolchain.cmake",
                      "-DANDROID_PLATFORM=33",
                      "-DAPP_PLATFORM=33",
                      "-DANDROID_ABI=arm64-v8a"
            cppFlags "-L/Users/username/AndroidStudioProjects/demo/app/src/main/cpp/fftw-3.3.10/.libs/ -lfftw3 -lm "
        }
    }
    ndk {
        abiFilters 'arm64-v8a'

    }
}

However, after including the library in my exec with #include "fftw-3.3.10/api/fftw3.h", I get the following error as soon as I want to call a related function ld: error: undefined symbol: fftw_malloc. Im aware that the application is not correctly linked to the library, but I don't know what's wrong with it, since I could successfully link other third party libraries this way.


Solution

  • I could solve the issue by including FFTW3 as a subdirectory:

    add_subdirectory(fftw-3.3.10)
    include_directories(fftw-3.3.10/api)
    

    and then linking it directly to the executable without adding it as a static library:

    target_link_libraries( exec fftw3 )