Using clang for cross-compiling - android

I'm working on x86_64-pc-linux-gnu. I'm using clang cross-compiler to generate executable for target aosp_x86_64.
Command used
clang -target x86_64-unknown-unknown-linux-android hello.c
Ended up with error
/usr/bin/ld: cannot find crtbegin_dynamic.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How can I proceed ?

Related

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

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.

CMake Android cross compiling: clang always try to link with gcc toolchain

I'm trying to build my CMake project to Android platform. I used CMake 3.12.3 and ninja 1.9.0, and belowing is the toolchain file I used:
set(CMAKE_SYSTEM_NAME "Android")
set(CMAKE_ANDROID_NDK "C:/Users/myname/AppData/Local/Android/Sdk/ndk/20.0.5594570")
set(CMAKE_ANDROID_ARCH_ABI "arm64-v8a")
set(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION "clang")
set(CMAKE_ANDROID_STL_TYPE "c++_static")
set(CMAKE_SYSROOT "${CMAKE_ANDROID_NDK}/platforms/android-24/arch-arm64")
set(CMAKE_C_ANDROID_TOOLCHAIN_PREFIX "${CMAKE_ANDROID_NDK}/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64/aarch64-linux-android")
set(CMAKE_CXX_ANDROID_TOOLCHAIN_PREFIX "${CMAKE_ANDROID_NDK}/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64/aarch64-linux-android")
It fails at configuration stage:
Android: Targeting API '24' with architecture 'arm64', ABI 'arm64-v8a', and processor 'aarch64'
Android: Selected Clang toolchain 'aarch64-linux-android-clang' with GCC toolchain ''
The C compiler identification is Clang 8.0.7
The CXX compiler identification is Clang 8.0.7
Check for working C compiler: C:/Users/myname/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
Check for working C compiler: C:/Users/myname/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.12/Modules/CMakeTestCCompiler.cmake:52 (message):
The C compiler
"C:/Users/myname/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: D:/projects/MyProject-android-arm64-ninja/CMakeFiles/CMakeTmp
Run Build Command:"D:/development/ninja-1.9.0/ninja.exe" "cmTC_70fcb"
[1/2] Building C object CMakeFiles/cmTC_70fcb.dir/testCCompiler.c.o
[2/2] Linking C executable cmTC_70fcb
FAILED: cmTC_70fcb
cmd.exe /C "cd . && C:\Users\myname\AppData\Local\Android\Sdk\ndk\20.0.5594570\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe --target=aarch64-none-linux-android --gcc-toolchain=C:/Users/myname/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains//prebuilt/windows-x86_64 --sysroot=C:/Users/myname/AppData/Local/Android/Sdk/ndk/20.0.5594570/platforms/android-24/arch-arm64 -funwind-tables -no-canonical-prefixes -D__ANDROID_API__=24 -fexceptions -g -fPIE -pie -Wl,--gc-sections CMakeFiles/cmTC_70fcb.dir/testCCompiler.c.o -o cmTC_70fcb && cd ."
ld: error: unable to find library -lgcc
ld: error: unable to find library -lgcc
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)
I noticed two unexpected behavior:
Although I specified CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION to clang, it still wants to use GCC libraries.
Although I specified C and CXX toolchain prefix, It still failed to to find binutils such as CMAKE_OBJDUMP, CMAKE_STRIP, CMAKE_OBJCOPY, CMAKE_NM.
So how should I make everything work?

CMake Compiler Check Fails when targeting LLVM build for Android NDK

I'm trying to cross-compile LLVM for the Android NDK, but my CMake invocation is failing. I'm using Ubuntu 16.04.
I've downloaded the LLVM source code (llvm version 3.8) and Android NDK (downloaded to a mounted disk, if that matters), and I've followed the instructions given in the Android.cmake file in [path to LLVM Source]/cmake/platforms:
# Android.cmake
# Toolchain config for Android NDK.
# This is expected to be used with a standalone Android toolchain (see
# docs/STANDALONE-TOOLCHAIN.html in the NDK on how to get one).
#
# Usage:
# mkdir build; cd build
# cmake ..; make
# mkdir android; cd android
# cmake -DLLVM_ANDROID_TOOLCHAIN_DIR=/path/to/android/ndk \
# -DCMAKE_TOOLCHAIN_FILE=../../cmake/platforms/Android.cmake ../..
# make <target>
SET(CMAKE_SYSTEM_NAME Linux)
IF(NOT CMAKE_C_COMPILER)
SET(CMAKE_C_COMPILER /usr/bin/clang)
ENDIF()
IF(NOT CMAKE_CXX_COMPILER)
SET(CMAKE_CXX_COMPILER /usr/bin/clang++)
ENDIF()
SET(ANDROID "1" CACHE STRING "ANDROID" FORCE)
SET(ANDROID_COMMON_FLAGS "-target arm-linux-androideabi --sysroot=${LLVM_ANDROID_TOOLCHAIN_DIR}/sysroot -B${LLVM_ANDROID_TOOLCHAIN_DIR}")
SET(CMAKE_C_FLAGS "${ANDROID_COMMON_FLAGS}" CACHE STRING "toolchain_cflags" FORCE)
SET(CMAKE_CXX_FLAGS "${ANDROID_COMMON_FLAGS}" CACHE STRING "toolchain_cxxflags" FORCE)
SET(CMAKE_EXE_LINKER_FLAGS "-pie" CACHE STRING "toolchain_exelinkflags" FORCE)
But the error I get when invoking CMake as cmake -DLLVM_ANDROID_TOOLCHAIN_DIR=/media/alvin/Windows8_OS/Android/Sdk/ndk-bundle -DCMAKE_TOOLCHAIN_FILE=../llvm-3.8.0.src/cmake/platforms/Android.cmake ../llvm-3.8.0.src is:
CMake Error at /usr/share/cmake-3.5/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "/usr/bin/clang" is not able to compile a simple test
program.
It fails with the following output:
Change Dir: /media/alvin/Windows8_OS/LLVM/and-build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_1e02b/fast"
/usr/bin/make -f CMakeFiles/cmTC_1e02b.dir/build.make
CMakeFiles/cmTC_1e02b.dir/build
make[1]: Entering directory
'/media/alvin/Windows8_OS/LLVM/and-build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_1e02b.dir/testCCompiler.c.o
/usr/bin/clang -target arm-linux-androideabi
--sysroot=/media/alvin/Windows8_OS/Android/Sdk/ndk-bundle/sysroot
-B/media/alvin/Windows8_OS/Android/Sdk/ndk-bundle -o
CMakeFiles/cmTC_1e02b.dir/testCCompiler.c.o -c
/media/alvin/Windows8_OS/LLVM/and-build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_1e02b
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_1e02b.dir/link.txt
--verbose=1
/usr/bin/clang -target arm-linux-androideabi
--sysroot=/media/alvin/Windows8_OS/Android/Sdk/ndk-bundle/sysroot
-B/media/alvin/Windows8_OS/Android/Sdk/ndk-bundle -pie
CMakeFiles/cmTC_1e02b.dir/testCCompiler.c.o -o cmTC_1e02b -rdynamic
/usr/bin/ld: unrecognised emulation mode: armelf_linux_eabi
Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu i386linux
elf_l1om elf_k1om i386pep i386pe
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
CMakeFiles/cmTC_1e02b.dir/build.make:97: recipe for target 'cmTC_1e02b'
failed
make[1]: *** [cmTC_1e02b] Error 1
make[1]: Leaving directory
'/media/alvin/Windows8_OS/LLVM/and-build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_1e02b/fast' failed
make: *** [cmTC_1e02b/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:54 (project)
It seems that there's a problem when linking: "clang: error: linker command failed with exit code 1", but I have no idea how to fix this. Does anyone have any insight on how to fix this problem?
EDIT: I'm using clang-3.8 and llvm source version 3.8 if that matters.

Cricket audio engine CMAKE

Did anyone implemeted Cricket audio engine (http://www.crickettechnology.com/blog/) for Android Studio using gradle and Cmake (Android NDK) ?
I have imported the lib in my Cmake like this:
target_link_libraries(test
${CMAKE_CURRENT_SOURCE_DIR}/../../test/cricketaudio/lib/libck.a)
And the compiler sees it alright but when I try to run it, the linker gives me this error:
Error while executing process
[1/1] Linking CXX shared library
..\..\..\..\build\intermediates\cmake\development\debug\obj\armeabi-
v7a\libtest.so
FAILED: cmd.exe -soname,libtest.so -o
..\..\..\..\build\intermediates\cmake\development\debug\obj\armeabi-
v7a\libtest.so #CMakeFiles/test.rsp && cd ."
core/system_android.cpp:174: error: undefined reference to
'android_getCpuFamily'
core/system_android.cpp:187: error: undefined reference to
'android_getCpuFeatures'
core/system_android.cpp:210: error: undefined reference to
'android_getCpuCount'
clang++.exe: error: linker command failed with exit code 1 (use -v to see
invocation)
ninja: build stopped: subcommand failed.
I am aware that libck (cricket audio) has a dependency lib the Android NDK cpufeauters lib which is located in the Android NDK bundle (Android\Sdk\ndk-bundle\sources\android\cpufeatures).
I don`t know how to make this cpufeatures lib visible to the Linker of my project ???
Any help or pointers would be appreciated.
I have simply included the cpu-features.c and header file to my cmakelist and now it works :).

Using SiftGPU in Android (about ndk, cmake and SSE)

Recently I am trying to use SiftGPU in Android. I am using the Linux. So, first I download a ndk and use the following code to get a standalone toolchain:
sudo sh ./build/tools/make-standalone-toolchain.sh --verbose --platform=android-15 --install-dir=/home/YourUserName/Downloads/my-tool --toolchain=arm-linux-androideabi-4.9
Then, I add the some codes to the CmakeLists.txt so it becomes:
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(SIFTGPU C CXX)
set(NDK_STANDALONE_TOOLCHAIN /home/YourUserName/Downloads/my-tool/)
set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 15)
set(CMAKE_C_COMPILER ${NDK_STANDALONE_TOOLCHAIN}/bin/arm-linux-androideabi-gcc)
set(CMAKE_CXX_COMPILER ${NDK_STANDALONE_TOOLCHAIN}/bin/arm-linux-androideabi-g++)
set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a)
set(CMAKE_FIND_ROOT_PATH ${NDK_STANDALONE_TOOLCHAIN})
add_definitions("--sysroot=${NDK_STANDALONE_TOOLCHAIN}/sysroot")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-write-strings -Wno-unused-result -Wno-deprecated -fPIC")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-write-strings -Wno-unused-result -Wno-deprecated -fPIC")
ADD_SUBDIRECTORY(src)
After that, I simply go to the directory of SiftGPU, create a new folder, enter it, and run:
cmake ..
Until now, everything seems ok. However, after I run the following command, well, unfortunately, it doesn't work:
make
And here is the error:
[ 9%] Building CXX object src/SiftGPU/CMakeFiles/siftgpu.dir/FrameBufferObject.cpp.o
arm-linux-androideabi-g++: error: unrecognized argument in option '-march=core2'
arm-linux-androideabi-g++: note: valid arguments to '-march=' are: armv2 armv2a armv3 armv3m armv4 armv4t armv5 armv5e armv5t armv5te armv6 armv6-m armv6j armv6k armv6s-m armv6t2 armv6z armv6zk armv7 armv7-a armv7-m armv7-r armv7e-m armv7ve armv8-a armv8-a+crc iwmmxt iwmmxt2 native
arm-linux-androideabi-g++: error: unrecognized command line option '-mfpmath=sse'
src/SiftGPU/CMakeFiles/siftgpu.dir/build.make:62: recipe for target 'src/SiftGPU/CMakeFiles/siftgpu.dir/FrameBufferObject.cpp.o' failed
make[2]: *** [src/SiftGPU/CMakeFiles/siftgpu.dir/FrameBufferObject.cpp.o] Error 1
CMakeFiles/Makefile2:103: recipe for target 'src/SiftGPU/CMakeFiles/siftgpu.dir/all' failed
make[1]: *** [src/SiftGPU/CMakeFiles/siftgpu.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
I tried to google it but I didn't get any useful result. It seems that the ndk complier is not compatible with SSE. Does anyone have any idea about this question?
Looks like their CMake files weren't meant to be cross-compiled.
arm-linux-androideabi-g++: error: unrecognized argument in option '-march=core2'
core2 is only valid for x86.
You'll need to take a look through SiftGPU's CMakeLists.txt to see if there are options for cross-compiling, and if not, just remove that flag.

Categories

Resources