Hello to all members of the forum.
I have just joined the NIM community and I need some help compiling a simple Hello program for Android with the NDK.
Here is the background to my problem
I am running Manjaro Linux x86_64 with Openbox
I have installed NDK in
/home/jimmy/Programs/android-ndk-r10e
I have installed a standalone NDK toolchain for arm64 in
/home/jimmy/Programs/android-toolchain-arm64
I have put in my .bashrc
PATH=/home/jimmy/Programs/android-toolchain-arm64/bin:"$PATH"
SYSROOT=/home/jimmy/Programs/android-ndk-r10e/platforms/android-21/arch-arm64/sysroot
arm64.linux.gcc.path = "/home/jimmy/Programs/android-toolchain-arm64/aarch64-linux-android/bin"
arm64.linux.gcc.exe = "aarch64-linux-android-gcc"
arm64.linux.gcc.linkerexe = "aarch64-linux-android-gcc"
Hello.nim which contains the line
echo "Hello world"
Here is the compilation problem
I compile from inside the Hello folder with the command
nim c --cpu:arm64 --os:linux --parallelBuild:1 --passC:--sysroot="$SYSROOT" --passL:--sysroot="$SYSROOT" Hello.nim
and I get the following error
Hint: system [Processing]
Hint: Hello [Processing]
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/include-fixed/syslimits.h:7:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/include-fixed/limits.h:34,
from /usr/lib/nim/nimbase.h:183,
from /home/jimmy/Developments/Nim/Hello/nimcache/Hello.c:9:
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/include-fixed/limits.h:168:61: error: no include path in which to search for limits.h
/home/jimmy/Developments/Nim/Hello/nimcache/Hello.c:10:19: fatal error: stdio.h: No such file or directory
compilation terminated.
Error: execution of an external program failed: 'gcc -c -w --sysroot=/home/jimmy/Programs/android-ndk-r10e/platforms/android-21/arch-arm64/sysroot -I/usr/lib/nim -o /home/jimmy/Developments/Nim/Hello/nimcache/Hello.o /home/jimmy/Developments/Nim/Hello/nimcache/Hello.c'
Can anybody please help?
https://github.com/yglukhov/nimx/blob/master/nimx/naketools.nim
Here's a nakefile that compiles to different platforms including Android. It calls nim with --compile-only flag, and then calls ndk-build.
Thank you Angluca and yglukhov for your answers but I have not managed to find a solution yet.
What I managed to do is to pass the folder with the missing header files as a parameter to the compiler:
nim c --cpu:arm64 --os:linux --d:android --parallelBuild:1 --passC:--sysroot="$SYSROOT" --passL:--sysroot="$SYSROOT" --passC:-I/home/jimmy/Programs/android-toolchain-arm64/sysroot/usr/include Hello.nim
but now I am getting a linker problem:
Hint: system [Processing]
Hint: Hello [Processing]
Hint: [Link]
/usr/bin/ld: this linker was not configured to use sysroots
collect2: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc -o /home/jimmy/Developments /Nim/Hello/Hello /home/jimmy/Developments/Nim/Hello/nimcache/system.o /home/jimmy/Developments/Nim/Hello/nimcache/Hello.o --sysroot=/home/jimmy/Programs/android-ndk-r10e/platforms/android-21/arch-arm64/sysroot -ldl'
Any ideas somebody?
Dear Jehan, the cross-compilation entry inn the compiler documentation is exactly what a I have followed.
Anyone else having an idea for solving this problem?
What does gcc say if you run it by hand?
/home/jimmy/Programs/android-toolchain-arm64/aarch64-linux-android/bin/aarch64-linux-android-gcc -o /home/jimmy/Developments /Nim/Hello/Hello /home/jimmy/Developments/Nim/Hello/nimcache/system.o /home/jimmy/Developments/Nim/Hello/nimcache/Hello.o --sysroot=/home/jimmy/Programs/android-ndk-r10e/platforms/android-21/arch-arm64/sysroot -ldl
/home/jimmy/Programs/android-toolchain-arm64/bin/aarch64-linux-android-gcc -o /home/jimmy/Developments/Nim/Hello/Hello /home/jimmy/Developments/Nim/Hello/nimcache/system.o /home/jimmy/Developments/Nim/Hello/nimcache/Hello.o --sysroot=/home/jimmy/Programs/android-ndk-r10e/platforms/android-21/arch-arm64/sysroot -ldl
I get:
/home/jimmy/Programs/android-toolchain-arm64/bin/../lib/gcc/aarch64-linux-android/4.9/../../../../aarch64-linux-android/bin/ld: cannot find crtbegin_dynamic.o: No such file or directory
/home/jimmy/Programs/android-toolchain-arm64/bin/../lib/gcc/aarch64-linux-android/4.9/../../../../aarch64-linux-android/bin/ld: /home/jimmy/Developments/Nim/Hello/nimcache/system.o: Relocations in generic ELF (EM: 62)
/home/jimmy/Programs/android-toolchain-arm64/bin/../lib/gcc/aarch64-linux-android/4.9/../../../../aarch64-linux-android/bin/ld: /home/jimmy/Developments/Nim/Hello/nimcache/system.o: Relocations in generic ELF (EM: 62)
/home/jimmy/Programs/android-toolchain-arm64/bin/../lib/gcc/aarch64-linux-android/4.9/../../../../aarch64-linux-android/bin/ld: /home/jimmy/Developments/Nim/Hello/nimcache/system.o: Relocations in generic ELF (EM: 62)
/home/jimmy/Developments/Nim/Hello/nimcache/system.o: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
Dear user "mr_july",
Can you please have a look at my thread and see if you can help me?
I have seen form this thread Build executable for Android with NDK, that you succeded in building an android executable despite the fact the it did not run due to library loading problems.
Stagiros: Dear Jehan, the cross-compilation entry inn the compiler documentation is exactly what a I have followed.
Well, it doesn't look as though the appropriate C cross-compiler and linker are being called. This would normally point to a problem in your configuration file, where the C compiler and linker aren't declared appropriately.
Dear Jehan,
This is my Hello.cfg file:
arm64.linux.gcc.path = "/home/jimmy/Programs/android-toolchain-arm64/bin"
arm64.linux.gcc.exe = "aarch64-linux-android-gcc"
arm64.linux.gcc.linkerexe = "aarch64-linux-android-gcc"
I have also tried changing the linker config like this:
arm64.linux.gcc.path = "/home/jimmy/Programs/android-toolchain-arm64/bin"
arm64.linux.gcc.exe = "aarch64-linux-android-gcc"
arm64.linux.gcc.linkerexe = "aarch64-linux-android-ld"
Can you see any errors?
@Stagiros: It's always difficult to tell with config files whether they're actually set up correctly and in the right place. What you need to do is to compile with --verbosity:2 --parallelbuild:1 -f to see what compiler and linker executables are actually being called and with what arguments and what config files are being used.
But I'm pretty positive that the ones you have declared are not being called, because even the error message in your first post talks about "gcc" failing, not "aarch64-linux-android-gcc". Looking closer, I suspect that Nim does not find your config file because it's named wrong. Your main options for a local config file are:
Dear Jehan you were wright.
Firstly, my .cfg file was named Hello.cfg when it should have been named Hello.nim.cfg.
Secondly, in my .bashrc file I had:
SYSROOT=/home/jimmy/Programs/android-ndk-r10e/platforms/android-21/arch-arm64/sysroot
when I should have had:
SYSROOT=/home/jimmy/Programs/android-toolchain-arm64/sysroot
After correcting these two, I compile with:
nim c --cpu:arm64 --os:linux --verbosity:2 --parallelBuild:1 -f --passC:--sysroot="$SYSROOT" --passL:--sysroot="$SYSROOT" Hello.nim
or even just with:
nim c --cpu:arm64 Hello.nim
and a native android executable is created.
Success!!!
Thank you very much.
Complementary info for other users.
In order to run it on your android device, execute in a bash shell form within the project Hello's dir the following commands:
adb devices
adb push Hello /data/tmp/Hello
adb shell /data/tmp/Hello
If at the last command you get:
error: only position independent executables (PIE) are supported.
recompile with:
nim c --cpu:arm64 --passC:-fPIE --passL:"-fPIE -pie" Hello.nim
and run again these bash commands:
adb push Hello /data/tmp/Hello
adb shell /data/tmp/Hello
Success!
In your bash terminal on your computer you will see the result of Hello running on your android device:
Hello world