loading...

How To Run C Programs on Android

bauripalash profile image Palash Bauri πŸ‘» ・Updated on ・3 min read

C is a well known Programming language Created by Dennis Ritchie between 1969 and 1973 atΒ Bell Labs, since then it has become one of the most widely used Programming Language of all time.

It is usually used for low level Programming such as developing Operating Systems, Drivers, well as variousΒ application softwareΒ for computers ranging fromΒ supercomputersΒ toΒ embedded systems.

The language has become available on a very wide range of platforms, from embedded microcontrollers toΒ supercomputers.

The C Programming

Android is based on Linux Kernel so it's definitely possible to compile & run C/C++ programs on Android.

C is quite cross-platform , so a C Program written in Windows can Run on Linux ( and android ) and vice versa.

Special Note : You might be wondering why i included C++ where we should be only focusing on C.
The Reason is, C++ was actually developed as a superset of C Programming Language, and nowadays nobody builds a compiler specific for only C. So a compiler for C can also compile C++ programs

If You're New To C Programming or want to start learning C Programming , i recommend reading our article, Baby Steps in C Programming

So With Further Talking Let's Jump into to find ways to run C/C++ Programms in Android

#1 CXXDroid

Developed by IIEC, CXXDroid is fully fledged C/C++ IDE for Android. It has quite powerful features listed below

  • Full Offline Compiler - No Internet Needed
  • Package Manager available to get libraries
  • Powerful Editor
  • C/C++ interpreter (REPL)
  • Code Examples

CXXDroid - Run C programs on Android

CXXDroid From Play Store
{: .crtx}

If you want to learn and experiment with C/C++ , I recommend using CXXDroid.

Install From : Play Store

#2 CppDroid

Developed by Anton Smirnov, CppDroid is a quite famous and well known C/C++ IDE for Android , it's robust and reliable which has many features,

  • Full Offline C/C++ compiler - No need of internet
  • Smart Syntax Highlighting
  • Auto Indentation
  • Themes

CppDroid - Run C & Cpp programs on Android

CppDroid from Play Store
{: .crtx}

If you want a full C/C++ Development workspace , I recommend using CppDroid.
But right Now there's one down point about it, It's not been updated in Play Store since 17 August,2017

Install it From : Play Store

Visit Their Official Website : https://www.cppdroid.info

#3 Termux

Termux, the all-in-one solution which was also mentioned on our previous article , Python For Android - Run Python Programs in Android

As also mentioned in previous article, it's a terminal emulator for Android which means with help of it we can run any linux (almost any πŸ˜‰) programs on Android.

editing C scripts in vim

termux.com
{: .crtx}

To Install and Use C/C++ compiler in Termux (in Termux clang is the C/C++ compiler) ,

  1. Download & Install Termux from : Play Store
  2. After Installing execute this command pkg install clang
  3. After Successfully installing clang you can compile C/C++ scripts.

Example :
clang hello.c
After you'll get an executable a.out which you can run using ~$./a.out

To edit and create C/C++ Scripts you can use VIM inside termux which can be installed with pkg install vim
Or, you can use QuickEdit

If you're new to the world of programming and never used Command Line Applications I wouldn't recommend you to use Termux , use CXXDroid or CppDroid instead.

So Friends That's It For Now, See You Next Time. Have and Questions , Suggestions? Let me know the comments below πŸ‘‡


If You Like My Work (My Articles, Stories, Softwares, Researches and many more) Consider Buying Me A Coffee β˜• πŸ€—

Discussion

pic
Editor guide
Collapse
koouty profile image
Kouty

Is it possible to make this for an unrooted device?

Collapse
bauripalash profile image
Palash Bauri πŸ‘» Author

Yes ofcourse. But still Root gives you more flexibility

Collapse
koouty profile image
Kouty

There is a problem for me. I am storing my files on sdcard because the device is unrooted. When I type in Termux 'clang filename.c' or 'clang filename.c -o filename', and enter, I have as result '$', no more. But I noticed that if there is an error in code, I have an error message.

It seems strange but one time the code were compiled.
I don't understand what is the problem.

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

Maybe it had Runtime Errors

Thread Thread
koouty profile image
Kouty

I don't understand

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

Can you share the source code of your program.
Runtime error are such type of error which can't be detected by compilers, and such error can be noticed while running the program

Thread Thread
koouty profile image
Kouty

I tried even the simplest code as hello world. It's running perfectly with Dcoder app.

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

But not compiling with Clang?

Thread Thread
koouty profile image
Kouty

Exactly

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

What error does it give?

Thread Thread
koouty profile image
Kouty

No error but doesn't compile

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

there must be something to identify the problem, without knowing the bug I can't help you to debug it.

Thread Thread
koouty profile image
Kouty

Here is a pasted text from Termux
$ gcc -o mult.c
clang-8: error: no input files
$ clang -o mult.c
clang-8: error:

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

Ah, seems your source code is not in Termux home path. You should copy your mult.c file to Termux home directory

Thread Thread
koouty profile image
Kouty

How can I make this?

Thread Thread
bauripalash profile image
Thread Thread
koouty profile image
Kouty

$ clang -o condition.c
clang-8: error: no input files
$ cd
$ ls
Filename eaain herbe.rb
conditiond.c eaain.c termux-sudo
downloads essain.c
$ pwd
/data/data/com.termux/files/home
$

Thread Thread
koouty profile image
Kouty

And this is in the path, right?

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

Ah now I understand.
Just Remove -o and it should work fine.
Do this clang condition.c

Thread Thread
koouty profile image
Kouty

I reinstalled Termux, clang and nano to write files. I wrote and saved a file in rb and a file in C. Note that I wrote in the Termux path and here is the result.I can run easily Ruby files but for c I don't reach out the expected result.

Setting up ruby (2.6.5) ...
$ nano simple.rb
$ ruby simple.rb
hello
$ nano simple.c
$ nano simple.c
$ clang nano.c
clang-8: error: no such file or directory: 'nano.c'
clang-8: error: no input files
$ clang simple.c
$ clang simple.c -o simple
$ ls
a.out downloads simple simple.c simple.rb storage
$ clang simple.c
$ clang -o simple.c
clang-8: error: no input files

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

C is a compiled language and It seems simple.c is compiled.
Just Run the binary simple with $ ./simple

Thread Thread
koouty profile image
Kouty

It works! So, if I understand, after the clang action, A compiled file is created, and I need to open it with prompting it, the same name of file without the ".c" at the end of the name. And to make this I need to type "./filename". Right?

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

Partially.
Look, C is a compiled langauge, and Clang is a compiler unlike Python or Ruby which are interpreted language.

When you type the command clang simple.c , clang takes the source code and compiles it into machine code into a.out (a.out is default output name) and to run the program you must execute the binary like this $ ./a.out

And you can name the output binary anything you like by simply passing the -o flag, the syntax is like this :

$clang <SOURCE_CODE> -o <OUTPUT_NAME>

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

@koouty , seems like you're new to C , I would recommend watching some Beginners' Tutorial and Reading some tutorials available on internet.

Thread Thread
koouty profile image
Kouty

Many many thanks.

Thread Thread
bauripalash profile image
Palash Bauri πŸ‘» Author

If any need any further help you can just DM me

Thread Thread
koouty profile image
Kouty

πŸ‘

Collapse
pmcgowan profile image
p-mcgowan

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
bauripalash profile image
Palash Bauri πŸ‘» Author

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 -

Collapse
therishu profile image
RASH

Will you please share how you had done that

Collapse
pmcgowan profile image
p-mcgowan

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
imprakharshukla profile image
Prakhar Shukla

Hey Palash, that was a great list indeed. But how about running a dedicated IDE for C on your non-rooted Android device? Andronix allows you to do just that in a few clicks.

Here's an article -

Collapse
pillowtalkpres1 profile image
PillowTalk Presents

Hey so I haven't programmed in a very long time last time I did was programming in C using windows api. Would I be able to write my C code using windows api and it still run on Android? Sorry if its a stupid question

Collapse
pillowtalkpres1 profile image
PillowTalk Presents

If not how would I create the interface? Sorry im so old lol if you can point me in the right direction that would be great.

Collapse
tux0r profile image
tux0r

Hmm. While I generally like the ability to perform C development on Android (thank you for the article!), I can't see myself writing actual code on small touch keyboards. :(

Collapse
bauripalash profile image
Palash Bauri πŸ‘» Author

I understand, but it wouldn't be a problem if you want to do some quick prototyping or experiment some little bit.

I wrote this article because a few years ago, I didn't have a computer just an android phone. I started learning to programme using the apps I mentioned above.

I hope above apps will be useful for people who don't have access to computers or want to do some quick prototyping.

Collapse
tux0r profile image
tux0r

They surely are! :)