The following instructions refer to the FindMax example. The two files you need for the example are
1. Write the Titanium interface first, by figuring out what functionality will be in C, and what will be in Titanium, and writing native method "stubs" to represent the native C function. For the FindMax example, the findMax method is a native method and it is declared as
public static native double findMax(double [1d] array);
The findMax method takes in a Titanium 1d array as argument, and returns the maximum element in the array.
2. Compile the code with the native stubs in it. For our FindMax example, the command is
You will get errors because the native code has not been written yet. Then, look in the generated code directory (tc-cache) for files named
T is the letter T. n is the number of letters in your Titanium class name. In the FindMax example, n would be 10. Name is the name of the Titanium class. For this example, the name is FindMax. So the file we would like to look at is T7findMax.h. In this file, you will see the translated C-language declarations (but not definitions) for all the native methods you have declared in the Titanium code. They will be long and ugly and mangled, but you can look closely at them and figure out which one was called what.
3. You need to copy the C-language declarations in the header file into a .c file and add code to them. So what you want to do is create a directory called "native" under the directory that has your Titanium code, and create a .c file in it with the same name as the header file in step 2. For the FindMax example, the C file would be T7FindMax.c. Note that the declarations in the C header file do not name the parameters, you will do that.
4. When you are done implementing the C code, you are ready to compile your Titanium code with your native code. For the FindMax example, the command is
tcbuild --cc-flags "-I." --ld-flags "-L./native" FindMax.ti
Below are some useful links on using native code in Titanium:
The C-language structure of Titanium array
Titanium Backend Specification
Native code examples in tlib library
Native code examples in PeTSC from Titanium
This document is a revision of a document written by CJ Lin. If you have questions, please send email to