3

I am working on macOS trying to build Android native source code using pure cmake command instead of the gradle external native build scripts. Below is the command I use to compile my Android C/C++ code:

➜ /Users/myname/Library/Android/sdk/cmake/3.6.4111459/bin/cmake \
-DCMAKE_LINKER=/Users/myname/Library/Android/ndk/android-ndk-r20b/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-ld \
-DCMAKE_TOOLCHAIN_FILE=/Users/myname/Library/Android/ndk/android-ndk-r20b/build/cmake/android.toolchain.cmake \
-DANDROID_STL=c++_static \
-DANDROID_TOOLCHAIN=clang \
-DANDROID_ABI=arm64-v8a \
-DANDROID_NDK=/Users/myname/Library/Android/ndk/android-ndk-r20b \
-DANDROID_PLATFORM=android-23 \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_MAKE_PROGRAM=/Users/myname/Library/Android/sdk/cmake/3.6.4111459/bin/ninja \
-G"Android Gradle - Ninja" \
..

By running above command, i always got below errors:

Run Build  
Command:"/Users/myname/Library/Android/sdk/cmake/3.6.4111459/bin/ninja"
"cmTC_2af97"

  [1/2] Building C object CMakeFiles/cmTC_2af97.dir/testCCompiler.c.o

  [2/2] Linking C executable cmTC_2af97

ld: unknown option:  
--sysroot=/Users/myname/Library/Android/ndk/android-ndk-r20b/toolchains/llvm/prebuilt/darwin-x86_64/sysroot


  clang: error: linker command failed with exit code 1 (use -v to see 
invocation)

  ninja: build stopped: subcommand failed.


  CMake will not be able to correctly generate this project. Call
Stack (most recent call first):   CMakeLists.txt:3 (project) 

After searching around, it looks like the ld linker from my macOS overwrites my NDK's ld linker. I have tried to pass the NDK linker ld via CMake argument as said in cmake-use-a-custom-linker by specifying below option to cmake:

-DCMAKE_LINKER=/Users/myname/Library/Android/ndk/android-ndk-r20b/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-ld

but still got above same error ld: unknown option: --sysroot xxx.

Does anyone have any ideas about how to solve this error? Appreciate your help.


Similar questions asked here: https://superuser.com/questions/1446914/ndk-r20-embedded-toolchain-fails-on-macos but not got answer yet.

shizhen
  • 12,251
  • 9
  • 52
  • 88
  • The answer you refer to also suggests modifying the `CMAKE_CXX_LINK_EXECUTABLE` variable with the new linker executable and flags. Did you do this? – Kevin Aug 19 '20 at 12:05
  • @squareskittles, thanks for your reply, i just tired with `-DCMAKE_C_LINK_EXECUTABLE=" -o " ` and `-DCMAKE_C_LINK_EXECUTABLE=" -o "` , both don't work and have the same errors seen. – shizhen Aug 19 '20 at 12:40
  • Please add this information to your question post, showing what values you substituted for each individual variable, such as ``, ``, ``, etc. – Kevin Aug 19 '20 at 12:42
  • [Documentation](https://developer.android.com/ndk/guides/cmake#variables) for Android toolchain describes `ANDROID_LD` option. You may try to set it to `lld`. By itself, variable `CMAKE_LINKER` has no meaning for CMake compilation/linking process. Setting `CMAKE_C_LINK_EXECUTABLE` could have a sense, but I don't sure whether CMake passes it to `try_compile` process in case of custom toolchain is used. – Tsyvarev Aug 19 '20 at 16:20

0 Answers0