Skip to content

Instantly share code, notes, and snippets.

@VirajKanse
Last active February 20, 2026 17:11
Show Gist options
  • Select an option

  • Save VirajKanse/1c0db872cd7685632c02f8826397f190 to your computer and use it in GitHub Desktop.

Select an option

Save VirajKanse/1c0db872cd7685632c02f8826397f190 to your computer and use it in GitHub Desktop.
GCC / Clang C/C++ Compiler On Android Using Termux (Linux Environment)
Install Termux App.
type "apt update"
Then We have to install a text editor to write our code so type "apt install vim" for vim text editor
or u can also use nano text editor for nano type "apt install nano"
Now Clang Installation type "apt install clang" and wait for download completes.
now to create a file simply type vim filename.c or .cpp or nano filename.c or cpp
then u will able to write ur code .
write.....to save ur code simply press esc then type ":wq"
now code is ready to compile
type "clang filename.c -o filename"
If ur code is right then it will be compiled.
Else it will show error.fix it.
To run ur program simply type "./filename".
Tada...Done.
1. apt update
2. apt install vim or nano
3. apt install clang
4. vim/nano filename.
5. clang filename.c -o filename
6. ./filename
@krlan2789
Copy link

clang++ filename.cpp -o filename

@JosiasAurel
Copy link

Hey please you said how to install GCC compiler in termux but that is clang. I prefer GCC

@VirajKanse
Copy link
Author

Hey please you said how to install GCC compiler in termux but that is clang. I prefer GCC

Gcc isn't available in termux pkgs
U have to deal with clang :V

@pdaxrom
Copy link

pdaxrom commented Aug 31, 2020

I added termux repo with gcc 10.2.0 (with fortran)
there how to install it https://cctools.info/index.php/Termux_repository_with_cctools_packages_(gcc_with_fortran_support_etc)

@debiplobi
Copy link

I tried to run a file in c making simply
clang filename.c -o filename but it won't run

gcc filename.c -o filename

@fahadali32
Copy link

Can i install some header file by this like color.h etc....

@GogoFC
Copy link

GogoFC commented Mar 30, 2021

Soo I'm here on this old thread.
I can't chmod a+x the file, it doesn't change anything. Device isn't rooted, so to execute files device needs to be rooted? Or otherwise how to execute a file since chmod isn't working.

Never mind, no need to chmod anything inside home directory of Termux, it runs fine and already is executable when compiled, but it doesn't work in other directories such as /sdcard which is probably a good thing :)

Copy link

ghost commented Apr 27, 2021

How to link libraries

@pdaxrom
Copy link

pdaxrom commented Jun 29, 2021

Not working

What are the errors during installation or use?

@pdaxrom
Copy link

pdaxrom commented Jun 30, 2021

Hi, letme check on the weekend, sorry

@Gustavo10Destroyer
Copy link

Gustavo10Destroyer commented Aug 27, 2021

Binary files can't run on Internal Storage, only on termux home folder

You need move the output to ~/ and use chmod +x filename

@AbdullahBinJahed
Copy link

Now GCC is available through its-pointless repo
pkg i gcc-11

@anik889
Copy link

anik889 commented Nov 26, 2021

Screenshot_2021-11-26-15-25-25-64
What is the problem here????
For a few days it started to show this kind of message while compiling the code

@MohdSaadJames
Copy link

@anik889 run pkg upgrade

@AhaomaMichael
Copy link

Screenshot_20220625-113421

@MohdSaadJames
Copy link

@AhaomaMichael type
apt update && apt upgrade
apt install clang
clang++ UserAge.cpp -o UserAge
./UserAge

@Tonylinks
Copy link

@AhaomaMichael type apt update && apt upgrade apt install clang clang++ UserAge.cpp -o UserAge ./UserAge

apt update && apt upgrade - Had couple of errors.
apt install clang - This returned an error "Unable to locate clang"

Any help would be highly appreciated. Thanks.

@Tonylinks
Copy link

@AhaomaMichael type apt update && apt upgrade apt install clang clang++ UserAge.cpp -o UserAge ./UserAge

apt update && apt upgrade - Had couple of errors. apt install clang - This returned an error "Unable to locate clang"
Any help would be highly appreciated. Thanks.

pkg up && pkg upg
pkg i build-essential

Thanks so much. The fault was actually from me. I was using the playstore version.
Am still very new in the tech space :)
Thanks for the support.

@69r70e6
Copy link

69r70e6 commented Feb 21, 2023

It's not considering iostream header file and conio file

@Aditya-AR-A
Copy link

Working fine for everything else but not working for this short prime number code.

//GNU nano 7.2 004prime.c
#include<stdio.h>
int main(){
int a,b;
printf("enter the number to check prime : ");
scanf("%d\n",&a);
for(b=2;b<=a-1;b++){
if(a%b==0){
printf("%d is not a prime",a);
break;}}
if (b==a){
printf("%d is a prime\n",a);}
return 0;}

@DevSaheerHost
Copy link

Permission denied

~/.../Download/CodingC $ ./a.out
bash: ./a.out: Permission denied
~/.../Download/CodingC $

@danddinh
Copy link

Soo I'm here on this old thread. I can't chmod a+x the file, it doesn't change anything. Device isn't rooted, so to execute files device needs to be rooted? Or otherwise how to execute a file since chmod isn't working.

The shared storage is mounted with noexec
copy the file to $HOME to chmod +x

@ZenoArrow
Copy link

Are you sure installing clang will give you GCC? Clang is the name of the LLVM C compiler. LLVM is a competitor to GCC.

@VirajKanse
Copy link
Author

VirajKanse commented Jan 6, 2025

Are you sure installing clang will give you GCC? Clang is the name of the LLVM C compiler. LLVM is a competitor to GCC.

This is 7yr old post mate, I didn't even know a shit 7yrs ago, anyway it should run a C code fr ;)

@ZenoArrow
Copy link

Are you sure installing clang will give you GCC? Clang is the name of the LLVM C compiler. LLVM is a competitor to GCC.

This is 7yr old post mate, I didn't even shit 7yrs ago, anyway it should run a C code fr ;)

If you want to install GCC on Termux, here's how you can do it...

pkg install tur-repo
pkg install gcc-14 gcc-default-14

That's it. Now you can use GCC to compile code. So instead of "clang filename.c -o filename" you would run "gcc filename.c -o filename".

@sudharshan04052008-code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <time.h>

int kbhit(void) {
struct termios oldt, newt;
int ch;
int oldf;

tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

ch = getchar();

tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);

if (ch != EOF) {
    ungetc(ch, stdin);
    return 1;
}

return 0;

}

int getch() {
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}

int width = 20, height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
int gameOver;

enum direction {STOP = 0, LEFT, RIGHT, UP, DOWN};
enum direction dir;

void Draw() {
system("clear");

for (int i = 0; i < width + 2; i++) printf("#");
printf("\n");

for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        if (j == 0) printf("#");

        if (i == y && j == x)
            printf("O");
        else if (i == fruitY && j == fruitX)
            printf("F");
        else {
            int printTail = 0;
            for (int k = 0; k < nTail; k++) {
                if (tailX[k] == j && tailY[k] == i) {
                    printf("o");
                    printTail = 1;
                }
            }
            if (!printTail) printf(" ");
        }

        if (j == width - 1) printf("#");
    }
    printf("\n");
}

for (int i = 0; i < width + 2; i++) printf("#");
printf("\nScore: %d\n", score);
printf("Use W A S D to Move | X to Exit\n");

}

void Setup() {
gameOver = 0;
dir = STOP;
x = width / 2;
y = height / 2;
srand(time(0));
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}

void Input() {
if (kbhit()) {
switch (getch()) {
case 'a': dir = LEFT; break;
case 'd': dir = RIGHT; break;
case 'w': dir = UP; break;
case 's': dir = DOWN; break;
case 'x': gameOver = 1; break;
}
}
}

void Logic() {
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;

for (int i = 1; i < nTail; i++) {
    prev2X = tailX[i];
    prev2Y = tailY[i];
    tailX[i] = prevX;
    tailY[i] = prevY;
    prevX = prev2X;
    prevY = prev2Y;
}

if (dir == LEFT) x--;
else if (dir == RIGHT) x++;
else if (dir == UP) y--;
else if (dir == DOWN) y++;

if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;

for (int i = 0; i < nTail; i++)
    if (tailX[i] == x && tailY[i] == y)
        gameOver = 1;

if (x == fruitX && y == fruitY) {
    score += 10;
    fruitX = rand() % width;
    fruitY = rand() % height;
    nTail++;
}

}

int main() {
Setup();
while (!gameOver) {
Draw();
Input();
Logic();
usleep(150000);
}
printf("Game Over! Final Score = %d\n", score);
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment