10

Background

  • OSX is OS
  • R8 NDK

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
DeltaCap019
  • 6,532
  • 3
  • 48
  • 70
Jackie
  • 21,969
  • 32
  • 147
  • 289

5 Answers5

8

You must tell GCC where to find the Android system files and headers. Either use:

  1. ndk-build and an Android.mk with BUILD_EXECUTABLE
  2. or, the --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
deltheil
  • 15,496
  • 2
  • 44
  • 64
  • Sysroot did not work, Android.mk not an option. I could get it to work if I made a standalone toolchain but isn't there a way to just link the req libs? – Jackie Jan 13 '13 at 22:27
  • 1
    sysroot **does** work (you should have made a mistake or a typo). The above solutions are variants to the same thing: tell your compiler where to find the system resources. No more, no less. And if you need to get detailed steps/flags officially used, just use `ndk-build V=1` to see the outputs. – deltheil Jan 13 '13 at 22:44
  • It didn't work here was my code... arm-linux-androideabi-gcc hello.c --sysroot=~/Development/Android/android-ndk-r8c/platforms/android-9/arch-arm – Jackie Jan 13 '13 at 22:54
  • In the context of this question the toolchain is provided by the Android NDK, so the corresponding `gcc` should *always* have been configured with `--with-sysroot`. – deltheil May 26 '16 at 20:52
6

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.

Kanwar Saad
  • 2,138
  • 2
  • 21
  • 27
Jackie
  • 21,969
  • 32
  • 147
  • 289
  • 1
    Also double check your application.mk file and make sure your platform version is higher than 8. – Jackie Jun 21 '13 at 15:16
  • 1
    I've needed some args more in a Ubuntu OS: /home/user/android-ndk-r10d/build/tools$ ./make-standalone-toolchain.sh --platform=android-21 --install-dir=/tmp/my-toolchain --toolchain=arm-linux-androideabi-4.9 --system=linux-x86_64 And for Mac OS X: /Users/user/android-ndk-r10d/build/tools$ ./make-standalone-toolchain.sh --platform=android-21 --install-dir=/tmp/my-toolchain --ndk-dir=/Users/user/android-ndk-r10d/ --toolchain=arm-linux-androideabi-4.9 --system=darwin-x86_64 – Neonigma Jan 29 '15 at 11:51
5

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.

VeRaj
  • 61
  • 1
  • 2
0

In my case, I needed an .o file and did not need to define main(). I had to specify the -c switch:

~/ax/arm-linux-androideabi-g++ --sysroot=~/an/platforms/android-8/arch-arm/ -c dummy.c

where ~/ax and ~/an are links:

ax -> ~/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin/
an -> ~/android-ndk-r9d/

did dummy.o.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

i solved the problem , the default compress file manager in ubuntu wasn't extracting symbolic links , so i tried : tar jxf filename.tar.bz2 to untar the ndk.tar.bz2 and now it works fine

Piyush
  • 5,607
  • 4
  • 27
  • 27