Sign in Join
Wiki copytemporary

FPC and Carbon

Warning: Apple removed the 32 bit Carbon framework from macOS 10.15 Catalina in 2019 at the same time as removing support for running any 32 bit software. For macOS 10.15 onwards, you must use the 64 bit Cocoa framework. See: Carbon Core Deprecations and Introduction to 64-Bit Guide for Carbon Developers

(macOS only)

(iOS only)

Carbon is a procedural API currently available on macOS and Mac OS Classic 8.1 or later. It can be utilized to create full-fledged native Macintosh applications integrated with its Aqua look. An alternative to the 32 bit Carbon API is Apple's 64 bit Cocoa API. This document offers specific information about how to use the Carbon API using FPC.

Pascal Bindings

The Carbon bindings are located in the unit MacOSAll, which comes with all standard installations of FPC on the Macintosh.

Carbon Tutorial

If you want to create a Carbon application without using LCL components, you'll have to use the Carbon API to achive this goal. This little tutorial shows how to create an application using only CarbonAPI functions, XCode and Interface Builder to create the user interface.

All Mac OS GUI applications must be provided as bundles. Bundle is specially formed set of folders and files, that contain all information necessary to run a application.

(if you're using Lazarus, you can create it easily Project Options -> Create Application Bundle);

To create a GUI application, will need to use Apple's Interface Builder utility. The utility is provided with the XCode tools and can be found at /Developer/Applications/. It's not necessary to use Interface Builder to create GUI applications, but IB makes it easier.

The Interface Builder may look familiar in some way. It includes a 'component palette' and Library and an 'object inspector.' To open the inspector, press cmd-shift-I or select it from the main menu Tools->Inspector)

if you're using the Finder you can access application folder by 'Show Package Content' if you're using the console command line interface, you can copy by the following command cp main.nib -r ./yourBundle.app/Contents/Resources/main.nib (where yourBundle.app is your application bundle name)

Now, let's return to the empty .pas file (or empty project file), that was perviously created. Set .pas code to the following

pascal
 uses
   MacOSAll;
        
 var
   err     : OSStatus;
   nibRef  : IBNibRef = nil;
   window  : WindowRef = nil;
     
 begin
   err := noErr;
    
   try
     // Create a Nib reference passing the name of the nib file (without the .nib extension)
     // CreateNibReference only searches into the application bundle.
     err := CreateNibReference(CFSTR('main'), nibRef);
     if err <> noErr then  Exit; 
              
     // Once the nib reference is created, set the menu bar. "MainMenu" is the name of the menu bar
     // object. This name is set in InterfaceBuilder when the nib is created.
     err := SetMenuBarFromNib(nibRef, CFSTR('MainMenu'));
     if err <> noErr then Exit; 
            
     // Then create a window. "MainWindow" is the name of the window object. This name is set in
     // InterfaceBuilder when the nib is created.
     err := CreateWindowFromNib(nibRef, CFSTR('Window'), window);
     if err <> noErr then Exit; 
        
   finally
     // We don't need the nib reference anymore.
     DisposeNibReference(nibRef);
   end;
     
   // The window was created hidden so show it.
   ShowWindow(window);
     
   // Running main message loop that starts the application.  
   RunApplicationEventLoop;
 end.

Remember, Free Pascal must know there the MacOSAll file is located (you should specify the path to it in /etc/fpc.cfg file) The command line is the following.

bash
fpc yourMainFile.pas -k-framework -kCarbon

if have not created a hardlink to the file, you'll need to create one or copy the executable (you'll also have to copy executable every time you rebuild the project);

bash
cp youMainFile ./youBundle.app/Contents/MacOS/

If you're using Lazarus, you can simply select Run->Build, you can also launch the application.


From the Free Pascal wiki, page "FPC and Carbon", as it stood on 2021-08-04 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.