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.