DEV Community

Discussion on: How To Run C Programs on Android

Collapse
 
pmcgowan profile image
p-mcgowan • Edited

Termux is amazing - I went on a business trip a while back and we got production error emails on a snowy back road in the middle of nowhere. I was able to ssh into the server, grep the log for the error, and push a hotfix with vim over mobile data. The team stopped making fun of me for programming on my phone after that.

I used TerminalIDE on my older rooted Galaxy S3, when Termux wasn't avail for that version (too lazy to re-root). I downloaded CPPDroid, stole the compiler, and modified it to work in the terminal so I could work on my C game at the time using vim. I spent a lot of time commuting to school, so I felt it was a better use of the time.

I still find it amazing that my new phone is more powerful than my old netbook I used to use in school, and invaluable to have a pocket *nix device with me at all times. I even set up a headless driver for ssh web dev on the fly. You get used to the tiny keyboard in time, most people just expect typos in my texts now...

Yeah, you need tiny fingers, but on a Pixel XL it's not the worst

Termux on Android

Collapse
 
therishu profile image
Rishu Pandey • Edited

Will you please share how you had done that?

Collapse
 
pmcgowan profile image
p-mcgowan • Edited

Stealing the compiler, or ssh'ing over data?

Either way - same answer. These days I use the Termux app (IIRC not available on iPhone), within which you can install openssh and the gcc toolchain which will allow you to ssh and build c programs just as you would on a nix terminal on any desktop.

When I stole the compiler, I had a rooted phone, installed the C4droid app, then went into it's package folder and looked for the binary and linked lib files, copied those into my home folder, made a small wrapper script that added all the necessary compiler flags, and built the c apps from the TerminalIDE terminal.

It's significantly more work (plus trial and error) doing it that way, and requires rooting your phone. Now, with Termux, it's a simple apt-get install -y build-essentials make gcc ... and apt-get install -y openssh-{client,server}.

EDIT: found the old script - left the comments for myself

usage="\
Usage: $(basename $0) [OPTIONS] FILES [-o OUTPUT]
      --libs  use library settings found in file.stdin
  -h, --help  show this menu
~/c4droid/com.n0n3m4.droidc/files/gcc/bin/arm-linux-androideabi-g++ INPUT -lm -ldl -llog -lz -std=c++11 -Wfatal-errors   -lncurses -I~/c4droid/com.n0n3m4.droidc/files/gcc/arm-linux-androideabi/include/ncurses -Wl,-allow-shlib-undefined
"

LIBS=

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
  echo "$usage"
  exit 1
elif [ "$1" == "--libs" ]; then
  LIBS="-lm -ldl -llog -lz -std=c++11 -Wfatal-errors -lncurses -I~/c4droid/com.n0n3m4.droidc/files/gcc/arm-linux-androideabi/include/ncurses -Wl,-allow-shlib-undefined"
  shift
fi

GPP=~/c4droid/com.n0n3m4.droidc/files/gcc/bin/arm-linux-androideabi-g++
#GCC=~/c4droid/com.n0n3m4.droidc/files/gcc/bin/arm-linux-androideabi-gcc

$GPP $LIBS $*
#$GCC $LIBS $*
exit $?

# droidc/files/file.stdin:
# cd /data/data/com.n0n3m4.droidc/files/gcc/tmpdir
# export TEMP=/data/data/com.n0n3m4.droidc/files/gcc/tmpdir
# /data/data/com.n0n3m4.droidc/files/gcc/bin/arm-linux-androideabi-g++ /data/data/com.n0n3m4.droidc/files/temp.c -lm -ldl -llog -lz -I"/storage/emulated/0/.cfiles/intconv" -std=c++11 -Wfatal-errors   -lncurses -I/data/data/com.n0n3m4.droidc/files/gcc/arm-linux-androideabi/include/ncurses -Wl,-allow-shlib-undefined -o /data/data/com.n0n3m4.droidc/files/temp
# echo "errorcode:$?"
# exit
Collapse
 
bauripalash profile image
Palash Bauri 👻 • Edited

Your experience was great. I remember last year I compiled the whole metasploit framework for Android (thanks to Termux) and it worked perfectly on multiple android devices. 😀

Collapse
 
imprakharshukla profile image
Prakhar Shukla

Termux is awesome indeed and we here at Andronix love it. Andronix is an open-source, completely ad-free app that lets you install full-fledged Linux distributions on your non-rooted Android device. We utilize the power of Termux 💙.

Here's an article -