Skip to content
RainbowC0 edited this page Dec 29, 2024 · 5 revisions

Basic

Open TermuC, click the bottom button and a buttom file list appear. Select a C/C++ source file or click the "+" button on the top-right of bottom list to create a new file.

Edit text. Write a "hello world" program (as following).

#include<stdio.h>

int main() {
  puts("Hello World");
  return 0;
}

Then click "Run" action button on the action bar. And you can see Termux pops up and print "Hello World".

Debug

Note

TermuC support breakpoint debug since v0.1.4, which currently just use GDB/CLI. This will get updated in the following versions.

Click the line number and it turn red, so you marked the line. Then run the program, choose the "Debug" option. It will call GDB CLI on Termux. You can control the procedure via GDB command.

Some command:

  • c[ontinue]: continue running until next breakpoint
  • n[ext]: execute one step
  • s[tep]: execute one step. If it's a function call, enter the function stack.
  • b[reak] <[filename:]number>: add a breakpoint
  • p[rint] <expression>: calculate the expression and print the value
  • d[elete] [all]|<id>: delete breakpoint
  • info b: show breakpoints
  • bt|backtrace: show call stacks
  • q[uit]: quit debugging

See also: https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gnat_ugn/Introduction-to-GDB-Commands.html

Tip

The debugger would not quit automatically. So please input q and press enter to close it manually.

Compile

You can config compile flags in settings. For example, to compile a program using Eigen library, set the compile flags with

-I$PREFIX/include/eigen3

or

`pkg-config --cflags --libs eigen3`

And then try such a C++ source:

#include <iostream>
#include <Eigen/Dense>

int main() {
  Eigen::MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << m << std::endl;
}

Clangd Configuration

The compile flags are not used for clangd, which adapt another configuration.

You can add a .clangd or compile_commands.json file in the directory including your source file(s). To add some compile flags for Eigen library, write a .clangd like this:

CompileFlags:
  Remove: [-xobjective-c++-header]
  Add: [-I/data/data/com.termux/files/usr/include/eigen3]

If your project use CMake, you can run cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 under the root path of your project in Termux to generate a compile_commands.json.

Note

Compile flags for clangd via both .clangd and compile_commands.json should be in completed string, while those for compile can follow the shell syntax.

See alse: https://clangd.llvm.org/config.html

Project

Project management is introduced since v0.1.5, which partly references the Geany project.

You can create a project like creating a single file, to click the bottom button and then the "+" button. Select "New project", and input the project name. After that it creates a directory with the project name, including a "termuc.json" file and a "build" directory. There is the basic structure:

project-name
├── build
└── termuc.json

TermuC would consider a directory as a project as long as finding a "termuc.json" file.

You can build, compile or run a project by click the "Run..." action menu, and customize the command line of all above via the "termuc.json" file.

The "termuc.json" saves some preferences of a project, which is in such a format:

{
  "buildCmd": shell-statement,
  "compileCmd": shell-statement,
  "runCmd": shell-statement,
  "outputDir": file-name,
  "opens": file-path[]
}

The formats are defined as following:

  • shell-statement: a valid shell statement string that can be executed by bash in Termux, which supports shell variables and syntax.
  • file-name: a valid file name string.
  • file-path: a valid absolute file path string.

Items of the "termuc.json" are:

  • buildCmd: The command to build the project. It should generate the ELF file. Default value clang $d/$f -o $o/$e -lm -Wall.
  • compileCmd: The command to compile the project or some modules. It should generate the object file (*.o). Default value clang -c $d/$f -o $o/$e.o -lm -Wall.
  • runCmd: The command to run the project. Default value $e.
  • outputDir: The directory under the project's root path that includes the ELF files. Default value build.

To make the command line more expressive, it provides some shell variables about the project:

  • $f: Name of the current file.
  • $e: $f without suffix.
  • $d: Directory path including the current file.
  • $p: Root path of the project.
  • $o: The output directory, according to outputDir.

Tip

If you want use Makefile or other build systems, you can easily set the buildCmd and compileCmd to make it.

Even if you want to run projects in other languages, such as Java, you can set buildCMD to javac $f or mvn compile, compile CMD to javac $f, run CMD to java $e.class or java -jar xxx, and so on.

Note

Since ELF programs can not be executed directly in external storage, they will be copied to a temporary directory in Termux's internal storage.

Therefore, you should ensure that the buildCmd generates the ELF files in the output directory for easy lookup and copying during execution. Morever, the actual running command has an extra $exec/ prefix compared to runCmd, where $exec is a variable pointing to the directory.

Clone this wiki locally