Migration from Android.mk to cmake - linking problem

455 views
Skip to first unread message

Tsvetan Bogoev

unread,
Jun 15, 2018, 2:48:30 AM6/15/18
to android-ndk
Hi, I have a problem while linking a release version of a shared library using cmake (gradle/Android studio)
While using Android.mk everything is working as expected - I am compiling a couple of static libraries using optimization level -Oz and LTO (-flto) and the it links fine.
But when I switched to cmake I use the same compilation options the code compiles but it can't be linked. Currently I can not find exact ld but it gives the following error:

arm-linux-androideabi/bin/ld: fatal error: Optimization level must be between 0 and 3
  clang
++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja
: build stopped: subcommand failed.

I did not managed how to pass this "-v" options
here are some gradle arguments and global flags from gradle

        externalNativeBuild {
            cmake
{
                arguments
"-DCMAKE_BUILD_TYPE=Release",
                       
"-DANDROID_CPP_FEATURES=rtti exceptions",
                       
"-DANDROID_STL=c++_static",
                       
"-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON"
                cppFlags
"-v -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -flto"
                cFlags  
"-v -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -flto"
           
}
       
}

Also I am not sure how to pass some options to linker only (like -Wl,--exclude-libs=ALL -Wl,--gc-sections) - I tried this

list(APPEND ANDROID_LINKER_FLAGS "-Wl,--exclude-libs=ALL -Wl,--gc-sections --verbose -flto")


I believe that there is something wrong with exact call to ld but since the build does not print it I can not compare it with the some call while using ndk-build/Andorid.mk
Message has been deleted

Tsvetan Bogoev

unread,
Jun 15, 2018, 5:05:53 AM6/15/18
to android-ndk
I am using r17b (also tried r17)
Message has been deleted

Tsvetan Bogoev

unread,
Jun 15, 2018, 9:03:28 AM6/15/18
to android-ndk
I found what causes the problem, while using cmake ld is called with the following options:
-plugin /home/myhome/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/LLVMgold.so
-plugin-opt=mcpu=generic
-plugin-opt=Os
-plugin-opt=-function-sections
-plugin-opt=-data-sections

when using ndk-build it looks like:
-plugin /home/myhome/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/LLVMgold.so
-plugin-opt=mcpu=generic

the "-plugin-opt=Os" is missing. So now I am trying to understand why cmake passes this to ninja an thus link command fails
Reply all
Reply to author
Forward
0 new messages