How to compile C code using NDK for Android Device (ARM)?
how to compile the above C code using NDK toolchain? I found a couple of similar links but none of them give simple and complete steps to do so.
If there is a link with complete steps please do refer me to the same.
Compile Environment: Ubuntu, compile should be done via console not any IDE
4 Answers 4
You’re right, I made a mistake, I had not even tested it and gave me the same error, is due to the entry point of the «main», as this has not changed but I hope this works for you. Anyway check the symbol table «nm», the real-time execution «strace», you can even use gdbserver.
#include int main (int argc, char *argv[]) < printf ("hello world"); return 0; >export NDK_ROOT=your_ndk_path export PATH=$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin:$PATH export CC=arm-linux-androideabi-gcc export LD=arm-linux-androideabi-ld arm-linux-androideabi-gcc -I$NDK_ROOT/platforms/android-18/arch-arm/usr/include -Wl,-rpath-link=$NDK_ROOT/platforms/android-18/arch-arm/usr/lib -Wl,-L$NDK_ROOT/platforms/android-18/arch-arm/usr/lib -Wl,-lc -o test test.c If ld return with erros like ". ld: error: cannot open. : No such file or directory" try this for your losed files: ln -s $NDK_ROOT/platforms/android-18/arch-arm/usr/lib/crtend_android.o ln -s $NDK_ROOT/platforms/android-18/arch-arm/usr/lib/crtbegin_dynamic.o
Thanks! But, could you please help me to understand «Anyway check the symbol table «nm», the real-time execution «strace», you can even use gdbserver.» I haven’t used these before
Thanks for the answer it works, but isn’t there a proper way to resolve these links:ln -s $NDK_ROOT/platforms/android-18/arch-arm/usr/lib/crtend_android.o ln -s $NDK_ROOT/platforms/android-18/arch-arm/usr/lib/crtbegin_dynamic.o ..also we don’t need -Wl,-rpath-link=$NDK_ROOT/platforms/android-18/arch-arm/usr/lib..is it there at runtime?
Well in a UNIX environment using bash and assuming NDK_ROOT is a variable, $NDK_ROOT access its value, I do not know why this does not work for you. -Wl is to specify to the compiler that «the next option» is for the linker.
-rpath-link is for using ELF or SunOS, the difference between «-rpath» and «-rpath-link» is that directories specified by «-rpath» options are included in the executable and used at runtime, whereas the -rpath-link option is only effective at link time.
Yes, but how will your -rpath-link work this path should be present on the target machine..without option too my code compiles successfully and what is the purpose of -Wl, -lc
I use gcc4.8 and android API-level18 usin this you can try: First way using command line. export NDK_ROOT=your_ndk_path export PATH=$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin:$PATH export CC=arm-linux-androideabi-gcc export LD=arm-linux-androideabi-ld export CPPFLAGS=-I$NDK_ROOT/platforms/android-18/arch-arm/usr/include export CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$NDK_ROOT/platforms/android-18/arch-arm/usr/lib/ -L$NDK_ROOT/platforms/android-18/arch-arm/usr/lib" export LIBS="-lc" arm-linux-androideabi-gcc -nostdlib -o test test.c If run "file test" you should see this: test: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped Second way using autotools. export PATH=$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin:$PATH ./configure --host=arm-linux-androideabi CC=arm-linux-androideabi-gcc LD=arm-linux-androideabi-ld CPPFLAGS="-I$NDK_ROOT/platforms/android-18/arch-arm/usr/include" CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$NDK_ROOT/platforms/android-18/arch-arm/usr/lib/ -L$NDK_ROOT/platforms/android-18/arch-arm/usr/lib" LIBS="-lc" make http://embelinux.blogspot.com/2013/09/autotools1-hola-mundo-la-autotools.html Third way using android developers ndk full feature. Read Android.mk file syntax specification. This document describes the syntax of Android.mk build file written to describe your C and C++ source files to the Android NDK. http://www.kandroid.org/ndk/docs/ANDROID-MK.html
Hi, could you please explain the CFLAGS and LDFLAGS..the compile fails..if I include
How to install android ndk in linux?
I have downloaded android NDK from here: http://developer.android.com/tools/sdk/ndk/index.html
for Linux 64-bit (x86) android-ndk-r10c-linux-x86_64.bin. How I do install it? The instructions don’t work. My OS is
57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
10 Answers 10
The Standard Way
Android’s NDK now ships as an self extracting executable. You likely need to set the executable bit:
$ chmod +x android-ndk-r10c-linux-x86_64.bin $ ./android-ndk-r10c-linux-x86_64.bin
The above will cause the NDK to extract into the current working directory.
Manual Extraction
Since the .bin file is really just a 7-Zip self extracting archive, you can manually extract the contents if needed:
$ 7za x -o/path/to/extract/to/ android-ndk-r10c-linux-x86_64.bin
7-Zip is available in Ubuntu for example via apt-get:
$ sudo apt-get install p7zip-full
Update
As of at least r14b on the NDK download page, we’re back to standard ZIP archives.
You may want to post some more information other than «It doesn’t work». See stackoverflow.com/help/how-to-ask
./android-ndk-r10c-linux-x86_64.bin: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
@Vardan95: That is answered here: stackoverflow.com/questions/11471722/… Alternatively, you can use the manual instructions I added.
and where can this .bin file be found? All I see are .zip files, (and no README to be seen anywhere, shame on you Google)
If you already have AndroidStudio installed:
You can install NDK using the SDK Manager from within Android Studio
From an open project, select Tools > Android > SDK Manager from the menu bar. Click the SDK Tools tab. Check the boxes next to LLDB, CMake, and NDK. Apply
ubuntu does not install by default Android Studio and nowhere in the question is mentioned that it is installed. So the answer is incorrect unless: First install Android Studio is provided before «install NDK using the SDK Manager from within Android Studio«
Another way to download and install (unpack) it (size of zip is ~820MB, unzipped is ~2.9G):
wget https://dl.google.com/android/repository/android-ndk-r20-linux-x86_64.zip unzip android-ndk-r20-linux-x86_64.zip
Go to the directory where you downloaded it. Then execute:
chmod +x android-ndk-r10c-linux-x86_64.bin ./android-ndk-r10c-linux-x86_64.bin
It should unpack right there. The toolchain will be in android-ndk-r10c-linux-x86_64/toolchains. The root folder might have a slightly different name.
Unfortunately this is not correct, because android-ndk-r10c-linux-x86_64.bin is an ELF executable, not a shell script.
Install 7-Zip via apt-get like this
$ sudo apt-get install p7zip-full
go to folder where your ndk bin is and then execute the command
I have androidBBQ which is archlinux based, installed using vmware. But I guess my instructions should be the same for any linux distribution. I used AndroidStudio’s built-in SDK manager, to try installing NDK. Download failed because there was no space in /tmp. Turns out that most Linux distros have ramdisk baesd tmpfs (temporary file system in RAM which is mounted at /tmp) and there is no separate partition which could be extended. /tmp is mounted usually by using 50% of available RAM.
Install NDK using Android Studio’s built-in SDK manager ->not so convenient
a) Extend /tmp
I edited my fstab as root, using this command :
I appended this line to my fstab
none /tmp tmpfs size=8G 0 0
Note: If no such line exists, add above line at end of fstab. If any such line exists, it means you had already extended your /tmp and it was still insufficient. So, only change the «size» value, by raising it by a few GBs.
Then I rebooted. Once I did, my /tmp was extended to 8GB size. For development purposes, I guess we should readily extend our /tmp because we will have to do it anyway some time later. Size of /tmp is not dependent on free space in our hard disk (whether virtual or physical).
b) run NDK install again
I ran the NDK install from within AndroidStudio again. It had to download the NDK zip afresh. Unfrotunate that there is no resume/recheck function to resurrect broken installs. My download speed was slow, so I minimized the virtual linux’s window and continued to read news. Later, I guess memory/page swapping occurred. The download failed. I restarted Linux and restarted NDK install the same way. This time, I let the virtual Linux machine stay on foreground. NDK installed fine with this log :
Manual NDK install in Linux (preferable) I am amazed that official Android dev section did not bother indicating exact install location of Android NDK. Android Studio looks for NDK at: