Cross‑Platform Development with CodeTyphon: Building Android and iOS Libraries
This article explains how to use the CodeTyphon IDE and Free Pascal Compiler to create reusable cross‑platform libraries, covering environment setup, compilation commands for Android, iOS devices and simulators, and a complete Pascal example with JNI bindings.
Many developers want to reuse a single code base on multiple platforms, keeping UI and system‑specific parts separate while sharing business logic, encryption, compression, and utility modules; this article explores how to achieve that with CodeTyphon.
CodeTyphon is a powerful, near‑universal cross‑platform development tool, and a related course is available on CC Talk (http://www.cctalk.com/course/161021604561/).
Using CodeTyphon to compile Android .so files is straightforward: install the tool, download the required libraries and toolchain, then compile the crosselements for arm‑android , mipsel‑android and i386‑android . Building for iOS is more involved because a Mac is required and CodeTyphon’s Mac support is limited.
On macOS, first obtain CodeTyphon (version 3.1.1) which contains the needed Free Pascal Compiler (FPC). The FPC source is also required to build an iOS‑capable cross‑compiler.
Define environment variables for the compiler and output paths. Example settings:
export FPC=/usr/local/codetyphon/fpc/fpc64/bin/x86_64-linux/fpc # Linux export FPC=/usr/local/codetyphon/fpc/fpc32/bin/i386-darwin/ppc386 # macOS (32‑bit for iOS) export OUT=/usr/local/lib/fpc/3.1.1 export IOS_PLATFORM=/Applications/Xcode.app/Contents/Developer/Platforms export TYPHON=/usr/local/codetyphonFull build steps (run in order):
svn checkout http://svn.freepascal.org/svn/fpc/trunk/Developer/FPC/3.1.1 cd /Developer/FPC/3.1.1 sudo make distclean sudo make FPC=${FPC} OPT="-ap" distclean all sudo make FPC=/Developer/FPC/3.1.1/compiler/ppc386 install sudo make distclean sudo make FPC=${FPC} CPU_TARGET="x86_64" OPT="-ap" distclean all sudo make crossinstall CPU_TARGET=x86_64 sudo mv ${OUT}/ppcrossx64 ${OUT}/ppcx64 export IOS_BASE=${IOS_PLATFORM}/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk export BIN_BASE=${IOS_BASE}/usr/bin sudo make FPC=${FPC} OPT="-ap" CPU_TARGET=aarch64 CROSSOPT="-FD${BIN_BASE} -XR${IOS_BASE}" all sudo make FPC=/Developer/FPC/3.1.1/compiler/ppcrossarm OPT="-ap" CPU_TARGET=aarch64 CROSSOPT="-FD${BIN_BASE} -XR${IOS_BASE}" install CROSSINSTALL=1After these steps the real‑device iOS toolchain is ready. To build a simulator version (x86_64) add:
export IOS_BASE=${IOS_PLATFORM}/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk export BIN_BASE=${IOS_BASE}/usr/bin sudo make FPC=${FPC} OPT="-ap" CPU_TARGET=x86_64 OS_TARGET=iphonesim CROSSOPT="-FD${BIN_BASE} -XR${IOS_BASE}" all sudo make crossinstall CPU_TARGET=x86_64 OS_TARGET=iphonesimExample Pascal library ( libdemo ) that exports a simple hello function and a JNI wrapper:
library libdemo; {$mode objfpc}{$H+} uses cthreads, ctypes, math, classes, sysutils, jni2, jni_utils; function hello(name: PChar): PChar; cdecl; var ret: string; begin ret := Format('Hello%s', [string(name)]); Result := strAlloc(Length(ret)); strcopy(Result, PChar(ret)); end; function Java_com_sample_NativeLib_hello(env: PJNIEnv; obj: jobject; name: jstring): jstring; stdcall; var namestr: string; ret: PChar; begin namestr := jstringToString(env, name); ret := hello(PChar(namestr)); Result := stringToJstring(env, string(ret)); end; exports hello, Java_com_sample_NativeLib_hello; begin SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]); end.Compile the library for Linux or macOS with:
fpc -Scghi demo.pasFor Android, add the target and architecture flags, e.g.:
fpc -Tandroid -Parm -Scghi -Fl${TYPHON}/bin/Libraries/android-4.4-api19-arm demo.pasTo produce an iOS static library (required because iOS rejects dynamic libraries), compile to object files and archive:
${OUT}/ppcrossa64 -Tdarwin -Cn demo.pas ar -q libdemo.a `grep "\.o$" link.res` ranlib libdemo.aFor the iOS simulator use the 64‑bit cross‑compiler:
${OUT}/ppcx64 -Tiphonesim -Cn demo.pasVerify that the hello symbol is present:
nm libdemo.a | grep helloFinally, link libdemo.a into an Xcode iOS project and add the appropriate header to call the function from Objective‑C or Swift.
Hujiang Technology
We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.