Search code examples
android-ndkcmakegoogle-project-tango

Linking Google Tango native SDK in a CMAKE configured NDK build


I'm trying to build a native library for Android using the Google Tango native SDK( Bernoulli ) using NDK r9d. I encounter an error during linking. I included libtango_client_api.so in my Android.mk file like this:

LOCAL_MODULE    := LocalModuleName

include $(CLEAR_VARS)
LOCAL_MODULE := tangoclientapi-prebuilt
LOCAL_SRC_FILES = lib/$(TARGET_ARCH_ABI)/libtango_client_api.so
LOCAL_EXPORT_C_INCLUDES := include
include $(PREBUILT_SHARED_LIBRARY)

LOCAL_STATIC_LIBRARIES := android_native_app_glue cpufeatures
LOCAL_SHARED_LIBRARIES := some-other-modules tangoclientapi-prebuilt

LOCAL_CFLAGS += -fexceptions -frtti -x c++ -D___ANDROID___ -DANDROID

include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/cpufeatures) 
$(call import-module,android/native_app_glue)

the CMAKE command I use looks like this:

cmake -DCMAKE_TOOLCHAIN_FILE="/path/to/android.toolchain.cmake" 
-DANDROID_NATIVE_API_LEVEL=9 
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.8 <some-other-defines> ..

the linker error I get after running 'make':

/path/to/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: 
error: DIV usage mismatch between ./obj/local/armeabi-v7a/libtango_client_api.so and output

this tells me that NDK is using arm-linux-androideabi-4.6 linker, am I right?

So experimentally I temporarily removed the toolchain folder and got the following during my reconfigured build:

Android NDK: There is no toolchain that supports the armeabi-v7a ABI.    
Android NDK: Please modify the APP_ABI definition in ./jni/Application.mk to use    
Android NDK: a set of the following values: armeabi armeabi-v7a armeabi-v7a-hard mips x86

the android.toolchain.cmake file I use is the one maintained by the OpenCV project from times of NDK r8d ( I simply added the necessary ANDROID_SUPPORTED_NDK_VERSIONS ): https://github.com/taka-no-me/android-cmake

Now to my questions.

  • How is it possible that 4.6 is used even though I specify 4.8?
  • Is it even possible to use libtango_client_api.so with NDKs prior to r10?

POST-ANSWER INSIGHT: As Jason pointed out the ANDROID_TOOLCHAIN_NAME cmake variable has nothing to do with the toolchain used when running 'ndk-build'. And as luck would have it I really had a post build command in my CMakeLists.txt that ran 'ndk-build' to build my JNI sources that used the static libraries built with 'make' and since I forgot to specify the 'NDK_TOOLCHAIN_VERSION' in Application.mk it used the default toolchain. Thanks a lot, Jason!


Solution

    • How is it possible that 4.6 is used even though I specify 4.8? Try to specify you NDK_TOOLCHAIN_VERSION in you APPLICATION.mk file. The -DANDROID_TOOLCHAIN_NAME argument to cmake should cause the CMake build to use GCC 4.8, but will have no effect on running ndk-build. 4.6 is the default.

    • Is it even possible to use libtango_client_api.so with NDKs prior to r10? As I mentioned in the comment, ibtango_client_api.so should be able to work with r9d, but haven't tested on any version before that.

    Also, it looks like you are mixing the ndk-build and CMake, if you want to build your own native code using CMake and link it to the client library, a simple target_link_libraries() in the CMakeLists.txt should suffice. The Android.mk above only makes sense if you are using ndk-build. For your native code, you should need to use one or the other of CMake and ndk-build, not both.