Running C code on android using termux or gnuroot?

Vekktone Official picture Vekktone Official · Sep 13, 2016 · Viewed 8.8k times · Source

I'm trying to run c code on my android device using various terminal emulator apps. I have tried 3 so far and all of them are giving me errors. The first one I tried was the most popular: terminal IDE. Ever since the later versions of android have been released this app will not compile c code due to a position independent executable (PIE) error. Next up was a promising app called termux. see the picture for and example of some of what I tried in this appThis app allows me to compile my C code with gcc but gives me "permission denied" errors whenever I try to run even the simplest of codes. I have examined and researched this error thoroughly. I have tried using chmod in various different ways as well as checking the permissions using ls -l and have tried compiling and running in both the external sd card AS WELL AS on the internal device storage. I understand that the SD card doesn't have exec permission but it still won't work even in my device's internal memory. I have asked for help from the termux creator but to no avail. No matter what I do I cannot seem to get these codes to run on my android device. It is not rooted and I don't plan to root it any time soon. All of these apps claim that they should work without rooting the device. I finally tried running C code in another app (my last hope) called gnuroot. Unfortunately I'm still getting the permission denied error, but the code does compile as it did in termux. I can't access any of the directories created by these apps by the way. They are hidden from the "my files" app which normally lets me browse directories on my device. However, the directories shown when I type in ls are not in the my files app, so I can't place any files directly in them. I can only see the basic home directories for termux and gnuroot, but when I try to place the code in there, I still get the permission denied error. Furthermore, I don't know what directory to even place the code in for it to work. I'm totally lost and really need help. Thanks.

Answer

Rahul picture Rahul · Nov 15, 2016

I've gotten C programs running well under termux using gcc.

While compiling, compile your code using the -o option, and no -c, like this:
gcc -o helloworld helloworld.c The object file helloworld will be created with execute permissions and you can run it with ./helloworld. Note that the source files must be in the home directory first. If you haven't changed your environment variables, first go to your home directory with cd and then compile your source files there.