Running C code in Termux compiled by clang

Viewed 6402

I used to compile my code with CppDroid on Android until last night.

After updating the app it began to show position independent executable error.

I reinstalled the older version but am still getting the same error.

I know it is a problem with Android 5.0 and above. But then how did CppDroid work till last night? Can anyone give me an answer or any clue?

Termux only provides Clang. Can anyone give me a way to successfully compile C code?

Permission denied error

enter image description here

1 Answers

Even though the file mode is 0777, Android does not allow files on shared storage to be executed by mounting with noexec.

If you run

/system/bin/mount | grep /storage

You'll find something like this:

/data/media on /storage/emulated type sdcardfs (rw,nosuid,nodev,noexec,noatime,low_uid=1023,low_gid=1023,gid=9997,multi_user,mask=0007,reserved=100MB)
                                                                ^~~~~~

The actual mount point may vary (/storage/sdcard0), but it's surely mounted with noexec.

You should move the executable to Termux's home directory (or somewhere in an ext4 mountpoint) before trying to execute it. That's what CppDroid does. It only runs executables in internal storage, though the source code may be stored anywhere.

If you want to run in Termux, follow this:

$ cp <your-program> ~/a.out
$ chmod 777 ~/a.out
$ ~/a.out
program output
$

For the "position independent executable (PIE)" issue, I suggest you clear data for CppDroid. It uses GCC as compiler, which requires -pie option to generate PIE executable. If you know what settings are messed up you can also try fixing it by yourself.

The Clang compiler provided by Termux generates PIE executables by default.

Related