Developing with Objective Pascal

Part 7: Including Pascal Static Libraries Inside an Objective-C Project


Writing a simple Pascal library
Building a simple example project
Next steps


These notes describe how to generate static libraries in Pascal language for the iOS plateform and how to include them into an Objective C project inside XCode 4. You should already have installed Xcode 4 and compiled the Free Pascal cross compilers for the iOS Simulator and the iOS devices, as explained by Phil Hess in the Part 3 and Part 4 of this tutorial series. All the command lines of this 6th part target the 2.5.1 version of the FPC compiler, so keep in mind that you might have to adapt them to the version you installed.

I take no credits for this tutorial, as it is marely the produce of my googling. Most of the credits hence fall to iCodeBlog for describing how to create static libraries in c/c++/ObjC for iOS, and to Jonas Maebe for explaining how to compile static libraries in Pascal for MacOS X and for providing an XCode example project mixing Objective C and Pascal.


Writing a simple Pascal library

  1. Using a text editor like TextWrangler copy and paste the following code into a file you'll name "paslib.pas".
    Library myPascalLibrary;
    { Adapted from Jonas Maebe's example project :
      To make c-compatible (and XCode-compatible) libraries, you must :
      1- use c-types arguments
      2- add a "cdecl" declaration after your functions declarations
      3- export your functions
      ctypes, math;
    { See 
      For a full list of c-types
    function FloatMultiplication(nb1, nb2: cDouble): cDouble; cdecl;
        FloatMultiplication := nb1 * nb2;
    function IntegerAddition(nb1, nb2: cint): cint; cdecl;
        IntegerAddition := nb1 + nb2;
    function DisplayString(myString: char):char; cdecl;
        DisplayString := myString;
      FloatMultiplication, IntegerAddition, DisplayString;
      {Do not remove the exception masking lines}
      SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]);
  2. Then, create and save the following c-header file, naming it "myPascalLibrary.h":
    extern double FloatMultiplication(double nb1, double nb2);
    extern int IntegerAddition(int nb1, int nb2);
    extern char DisplayString(char myString);
  3. Save the two files into a drawer on your desktop, that you will for instance name "paslib", open a terminal and navigate to this drawer, and compile it by entering the following command (you might want to add some more switches, like those for optimizing):
    /usr/local/lib/fpc/2.5.1/ppcross386 -Tiphonesim -Cn paslib.pas
    This will create a ".o" object and a bunch of other files.
  4. Now convert this ".o" file into a ".a" static library usable by XCode by entering the following commands:
    ar -q paslib.a `grep "\.o$" link.res`
    ranlib paslib.a
There you are! You just built a static library in Pascal that you can use in XCode to generate Objective-C projects for the iOS simulator.

Building a simple example project

We will build a very simple Objective-C application with a simple button that triggers a doCalculation method when tapped. This doCalculation method will simply use the three functions of our Pascal library and display the result in Xcode's log window using NSLog.
  1. Launch XCode 4 and create a new iOS "view-based" project that you will name testlib.
  2. In Xcode's file menu, chose "Add Files to testlib..." and import the ".h" and ".a" files we've just created. Make sure to check the box asking if XCode should copy those files into the project's drawer.
  3. Now add the following import line to the testlibViewController.m file:

    #import "myPascalLibrary.h"
  4. Then edit the testlibViewController.h file so it looks like the following snippet (you actually don't really need an IBOutlet for the button, as we won't have to access it from our code, it's up to you, so only the IBAction is mandatory):

    #import <UIKit/UIKit.h>
    @interface testlibViewController : UIViewController {
        IBOutlet UIButton *myButton;
    @property (retain, nonatomic) UIButton *myButton;
  5. Now go back to testlibViewController.m and add the following method:

    -(IBAction)doCalculation:(id)sender {
        NSLog( @"Multiplication: %f", FloatMultiplication(2.0, 3.0) );
        NSLog( @"Addition: %i", IntegerAddition(5,7));
        NSLog( @"Display string: %@", @"Oh boy! We did it again!");
  6. It's now time to focus on our project's xib file: add a button to the view, ctr-drag from it to the file's owner and chose "doCalculation", then ctrl-drag from the file's owner to the button and chose "myButton".
  7. There you are! Save everything and, making sure that you target the iOS simulator, build and run the project, tap the button and look at what happens in XCode:

    2011-08-10 20:55:58.670 testlib[1724:207] Multiplication: 6.000000
    2011-08-10 20:55:58.672 testlib[1724:207] Addition: 12
    2011-08-10 20:55:58.673 testlib[1724:207] Display string: Oh boy! We did it again!

Next steps

This project will only work inside the iOS Simulator. To get it working on the real thing, follow those steps:
  1. Compile your pascal library using this line instead:
    /usr/local/lib/fpc/2.5.1/ppcrossarm -Cfvfpv2 -FD/Developer/Platforms/iPhoneOS.Platform/Developer/usr/bin -Cn paslib.pas
    Don't forget to make it a ".a" file using the commands we used above.
  2. Rename the paslib.a in your testlib project into paslib.a.sim, so you can rename it back when you need to run your project into the simulator again.
  3. Copy the newly built paslib.a file in the same drawer as paslib.a.sim.
  4. As the FPC compiler produces only armv6 binaries, you have to change the default target architecture of your project. To do so, go to your project settings -> build settings -> architectures click on the "Standard (armv6 armv7)" and set it to "other". In the pop up window, double click on the default line, erase it, and just replace it with "armv6". Click done.
  5. Plug in your iOS device, click run, and verify that it works.

Copyright 2011 by Phil Hess and Julien Marcel.

macpgmr (at) fastermac (dot) net

First posted August 10, 2011; last edited Aug. 10, 2011.