Background
I am trying to compile the following class using the Android GCC compiler...
#include <stdint.h>
int main (void){
return 0;
}
I do the with the following command...
un@un:~/Development/Code/OpenGL$ ~/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi-gcc hello.c -o hello
I get...
In file included from hello.c:1:0:
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/include/stdint.h:3:26: fatal error: stdint.h: No such file or directory
compilation terminated.
So due to a lack of gcc knowledge (but some Google ability) I find this and try it...
un@un:~/Development/Code/OpenGL$ ~/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi-gcc hello.c -o hello -ffreestanding
and I get...
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtbegin_dynamic.o: No such file or directory
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtend_android.o: No such file or directory
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lc
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: ld returned 1 exit status
Can someone help me with what I am doing wrong? Am I missing a link or something? Android.mk is not an option.
UPDATE this isn't working either...
arm-linux-androideabi-gcc hello.c --sysroot=~/Development/Android/android-ndk-r8c/platforms/android-9/arch-arm
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtbegin_dynamic.o: No such file or directory
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtend_android.o: No such file or directory
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lc
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: ld returned 1 exit status
You must tell GCC where to find the Android system files and headers. Either use:
ndk-build
and an Android.mk
with BUILD_EXECUTABLE
--sysroot
GCC option[1]
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo.c
include $(BUILD_EXECUTABLE)
[2]
# Change `android-9` with the level you target
/path/to/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt\
/darwin-x86/bin/arm-linux-androideabi-gcc\
--sysroot /path/to/android-ndk-r8c/platforms/android-9/arch-arm/\
foo.c -o foo
# Or generate a ready-to-use standalone toolchain (better)
/path/to/android-ndk-r8c/build/tools/make-standalone-toolchain.sh \
--platform=android-9 \
--install-dir=/tmp/my-android-toolchain
export SYSROOT=/tmp/my-android-toolchain/sysroot
/path/to/arm-linux-androideabi-gcc --sysroot $SYSROOT foo.c -o foo
So, since I don't want to use Android.mk file, I went ahead and created a standalone toolchain. this is done using the following...
/Users/un/Downloads/android-ndk-r8d/build/tools/make-standalone-toolchain.sh --platform=android-9 --install-dir=/tmp/my-toolchain
/tmp/my-toolchain/bin/arm-linux-androideabi-gcc hello.c
I would like to know what the "alternative" is in terms of the gcc linking I could do.
This answer adds a bit more details to @deltheil's answer. I had similar issues as I was trying to compile I2C-tools for debugging I2C bus on Android. Somehow after struggling for more than a day with make files and trying different options including --sysroot & --dynamic-linker options etc., I finally tried to compile it within the Android AOSP tree. I used the Google Nexus-S AOSP to build a binary that I intended to run on Samsung S3 phone. I created a folder called i2c-tools for the sources inside the AOSP/external folder and copied Android.mk, Cleanspec.mk & MODULE_LICENCE from another executable folder (ping) and modified it for i2c-tools as follows:
ifneq ($(TARGET_SIMULATOR), true)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := i2cdetect.c i2cbusses.c
LOCAL_C_INCLUDES := $(KERNEL_HEADERS)
LOCAL_MODULE := i2cdetect
LOCAL_MODULE_TAGS := tests
LOCAL_SHARED_LIBRARIES := libc
include $(BUILD_EXECUTABLE)
endif
Then I just ran:
source build/envsetup.sh
make i2cdetect
from the AOSP base folder and voila, I had a working executable in out/target/product/generic/system/bin/ folder. Note that I had to copy all needed source and header files from the original (i2c-tools)/tools & include folders and had to modify some of the #include to remove the extra path for header files that were now in the same place as the c-source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With