I tried to compile a test program following the steps indicated in this post, but it does not work.
$ nim c --cpu:arm64 --passC:-fPIE --passL:"-fPIE -pie" test.nim
$ adb devices
List of devices attached
42044158c4bc8100 device
$ adb push test /storage/sdcard0/Download/test
$ adb shell /storage/sdcard0/Download/test
/system/bin/sh: /storage/sdcard0/Download/test: can't execute: Permission denied
Any suggestion?Not work :-(
$ adb push test /storage/sdcard0/Download/test
$ adb shell chmod a+x /storage/sdcard0/Download/test
Bad mode
$ adb shell
10|shell@android:/ $ chmod 0755 /storage/sdcard0/Download/test
Unable to chmod /storage/sdcard0/Download/test: Operation not permitted
/system/bin/sh: /storage/sdcard0/Download/test: can't execute: Permission denied
Have you:My understanding is that you have to do this before you try to run self-installed, self-made software via adb instead of AndroidStudio. Even when you build a package through AndroidStudio, you have to enable these things before the device will let you run software you install yourself.
Yes I enable developer options and software from untrusted sources.
I discovered that the problem is to write on the sdcard0. If I change the location and try to run it gives another error: not executable: magic 7F45
$ adb push test /data/local/tmp/test
$ adb shell /data/local/tmp/test
/system/bin/sh: /data/local/tmp/test: not executable: magic 7F45
If I change the location and try to run it gives another error: not executable: magic 7F45
That sounds like the executable is not built correctly. I hope someone helps you find a solution, because I'm also interested in using Nim to build Android executables. Kotlin is pretty good, but it would be great to use native code.
I tried to follow the recipe of this post but without success :-(
$ wget https://dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip
$ unzip android-ndk-r19c-linux-x86_64.zip
S YSROOT=/home/hdias/Downloads/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot
$ nim c --cpu:arm64 --os:linux --verbosity:2 --parallelBuild:1 -f --passC:--sysroot="$SYSROOT" --passL:--sysroot="$SYSROOT" test.nim
...
Hint: gcc -c -w --sysroot=/home/hdias/Downloads/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot -I/home/hdias/.choosenim/toolchains/nim-0.19.4/lib -o /home/hdias/.cache/nim/test_d/stdlib_system.c.o /home/hdias/.cache/nim/test_d/stdlib_system.c [Exec]
In file included from /home/hdias/Downloads/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/sys/types.h:36,
from /home/hdias/Downloads/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/bits/strcasecmp.h:33,
from /home/hdias/Downloads/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/string.h:36,
from /home/hdias/.cache/nim/test_d/stdlib_system.c:11:
/home/hdias/Downloads/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/linux/types.h:21:10: fatal error: asm/types.h: No such file or directory
#include <asm/types.h>
^~~~~~~~~~~~~
compilation terminated.
Error: execution of an external program failed: 'gcc -c -w --sysroot=/home/hdias/Downloads/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot -I/home/hdias/.choosenim/toolchains/nim-0.19.4/lib -o /home/hdias/.cache/nim/test_d/stdlib_system.c.o /home/hdias/.cache/nim/test_d/stdlib_system.c'
With latest android-ndk and the information from this post
#!/bin/sh
mkdir tmp
cd tmp
cp ../test.nim ./
git clone https://github.com/nim-lang/nim
wget https://dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip
unzip android-ndk-r19c-linux-x86_64.zip
nim c --os:android --cpu=arm --os:android --compileOnly test.nim
NDK="/home/hdias/tmp/android-ndk-r19c"
$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi28-clang \
-I$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot \
-I /home/hdias/tmp/nim/lib -fPIE -pie \
-o /home/hdias/tmp/test.bin /home/hdias/.cache/nim/test_d/test.c /home/hdias/.cache/nim/test_d/stdlib_system.c
adb push test.bin /data/local/tmp/test.bin
adb shell /data/local/tmp/test.bin
I get this error with cpu=arm:
reloc_library[1307]: 4594 cannot locate '__register_atfork'...
CANNOT LINK EXECUTABLE
With cpu=arm64 I get:
not executable: magic 7F45
I finally succeeded :-) My android test phone is old. I found the solution here Work with API level 19!
test.bin: 1 file pushed. 1.2 MB/s (142260 bytes in 0.109s)
Hello World!
Cosmetic changes in sh script:
#!/bin/sh
NIMSRC="https://github.com/nim-lang/Nim/archive/v0.19.4.zip"
NDKSRC="https://dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip"
TESTCODE="/home/hdias/test.nim"
APILEVEL=19
CPU="arm"
TMPDIR="/home/hdias/tmp"
NIMCACHE="/home/hdias/.cache/nim"
CLANG="armv7a-linux-androideabi$APILEVEL-clang"
if ! [ -d $TMPDIR ]
then
mkdir $TMPDIR
fi
cd $TMPDIR
cp $TESTCODE ./
if [ -z $(ls -d1 Nim-*/) ]
then
wget $NIMSRC
unzip $(ls -1 v*.zip)
fi
NIM=$(ls -d1 Nim-*/)
NIM=$TMPDIR/${NIM::-1}
if [ -z $(ls -d1 android-ndk-*/) ]
then
wget $NDKSRC
unzip $(ls -1 android-ndk-*.zip)
fi
NDK=$(ls -d1 android-ndk-*/)
NDK=$TMPDIR/${NDK::-1}
NIMFILE=$(basename -- $TESTCODE)
nim c --os:android --cpu=$CPU --os:android --compileOnly $NIMFILE
FILENAME="${NIMFILE%.*}"
$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/$CLANG \
-I$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot \
-I$NIM/lib -fPIE -pie \
-o $TMPDIR/$FILENAME.bin \
$NIMCACHE/$FILENAME"_d"/$FILENAME.c $NIMCACHE/$FILENAME"_d"/stdlib_system.c
if [ -z $(adb get-state) ]
then
echo "Error!"
exit
fi
adb push test.bin /data/local/tmp/test.bin
adb shell /data/local/tmp/test.bin