DEV Community

Stephano Kambeta
Stephano Kambeta

Posted on

How to Compile and Run C/C++ Code on Android with Termux

If you’ve ever wanted to code in C or C++ directly from your Android device without a bulky laptop, Termux makes it possible. With the right packages, you can compile and run your programs right from your phone — perfect for students, programmers on the go, or anyone experimenting with systems programming.

This guide will walk you through setting up a mobile C/C++ development environment. If you’ve tried other Termux setups like quick automation projects or networking tools, you’ll find this equally straightforward and fun.

Why Compile C/C++ in Termux?

  • Portable coding – Code anywhere without needing a full PC.
  • Low-level control – C/C++ let you work close to the hardware.
  • Test algorithms – Quickly test code snippets before deploying to a bigger system.
  • Integration – Combine C programs with Termux scripts for automation.

For students, this setup can complement other programming-focused Termux workflows, just like security planning scripts complement network safety.

Step 1: Update Termux and Install Packages

Start by updating Termux and installing the GCC compiler for C/C++:

pkg update && pkg upgrade -y
pkg install clang -y
pkg install build-essential -y

Enter fullscreen mode Exit fullscreen mode

Here, clang is the compiler, and build-essential provides tools like make for more complex projects. This is similar to preparing dependencies before running tools like Ngrok or Nmap.

Step 2: Create Your First C Program

Let’s write a simple “Hello, World!” program in C. Create a file using the nano editor:

nano hello.c

Enter fullscreen mode Exit fullscreen mode

Paste this code inside:

#include <stdio.h>

int main() {
    printf("Hello, Termux!\\n");
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Save the file with CTRL + O and exit with CTRL + X.

Step 3: Compile and Run the C Program

Compile your program with:

clang hello.c -o hello

Enter fullscreen mode Exit fullscreen mode

Then run it:

./hello

Enter fullscreen mode Exit fullscreen mode

You should see:

Hello, Termux!

Enter fullscreen mode Exit fullscreen mode

Step 4: Create a C++ Program

If you prefer C++, create a new file:

nano hello.cpp

Enter fullscreen mode Exit fullscreen mode

Paste this code inside:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello from C++ in Termux!" << endl;
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Compile it with:

clang++ hello.cpp -o hello_cpp

Enter fullscreen mode Exit fullscreen mode

Then run:

./hello_cpp

Enter fullscreen mode Exit fullscreen mode

Step 5: Running Larger Projects

For multi-file projects, you can use make to compile everything at once. Create a Makefile to define build rules — similar to how you’d automate workflows in OT security scripting or security framework implementations.

Step 6: Combining C/C++ with Termux Scripts

You can integrate compiled programs with Termux automation. For example, a C program could gather system info, then a Bash script could send it via Telegram, just like in our MaxPhisher demo or network monitoring scripts.

Security Considerations

While C/C++ gives you power, it also gives you responsibility:

  • Be careful with file paths and permissions — especially when accessing sensitive directories.
  • Validate inputs to avoid buffer overflows or code injection issues.
  • Run suspicious code in isolated environments, just as you would when testing unknown scripts from security advisories.

Practical Uses

  • Build lightweight command-line tools for Android.
  • Run high-performance calculations without relying on heavy mobile apps.
  • Compile networking utilities for quick field testing.
  • Create educational programs for learning data structures and algorithms.

Conclusion

With just a few steps, Termux turns your Android phone into a portable C/C++ development environment. Whether you’re learning to code, optimizing algorithms, or creating custom utilities, compiling natively on Android is both practical and empowering. And when paired with other Termux skills from our project ideas, you’ll have a truly versatile mobile coding setup.

Top comments (0)