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 # Linuxexport FPC=/usr/local/codetyphon/fpc/fpc32/bin/i386-darwin/ppc386 # macOS (32‑bit for iOS)export OUT=/usr/local/lib/fpc/3.1.1export IOS_PLATFORM=/Applications/Xcode.app/Contents/Developer/Platformsexport TYPHON=/usr/local/codetyphonFull build steps (run in order):
svn checkout http://svn.freepascal.org/svn/fpc/trunk/Developer/FPC/3.1.1cd /Developer/FPC/3.1.1sudo make distcleansudo make FPC=${FPC} OPT="-ap" distclean allsudo make FPC=/Developer/FPC/3.1.1/compiler/ppc386 installsudo make distcleansudo make FPC=${FPC} CPU_TARGET="x86_64" OPT="-ap" distclean allsudo make crossinstall CPU_TARGET=x86_64sudo mv ${OUT}/ppcrossx64 ${OUT}/ppcx64export IOS_BASE=${IOS_PLATFORM}/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdkexport BIN_BASE=${IOS_BASE}/usr/binsudo make FPC=${FPC} OPT="-ap" CPU_TARGET=aarch64 CROSSOPT="-FD${BIN_BASE} -XR${IOS_BASE}" allsudo 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.sdkexport BIN_BASE=${IOS_BASE}/usr/binsudo make FPC=${FPC} OPT="-ap" CPU_TARGET=x86_64 OS_TARGET=iphonesim CROSSOPT="-FD${BIN_BASE} -XR${IOS_BASE}" allsudo 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;beginret := 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;beginnamestr := jstringToString(env, name);ret := hello(PChar(namestr));Result := stringToJstring(env, string(ret));end;exports hello, Java_com_sample_NativeLib_hello;beginSetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]);end.Compile the library for Linux or macOS with: fpc -Scghi demo.pas For 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.pasar -q libdemo.a `grep "\.o$" link.res`ranlib libdemo.aFor the iOS simulator use the 64‑bit cross‑compiler: ${OUT}/ppcx64 -Tiphonesim -Cn demo.pas Verify that the hello symbol is present: nm libdemo.a | grep hello Finally, link libdemo.a into an Xcode iOS project and add the appropriate header to call the function from Objective‑C or Swift.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
