macOS Frameworks

From Lazarus wiki
Jump to navigationJump to search

todo: please write a better description of what the framework is

macOS provides a unique way for distributing dynamic libraries: Frameworks.

Frameworks are actually a set of dynamic libraries and corresponding resources to them. A special kind of Frameworks are system Plugins (or drivers).

All macOS system APIs come in the form of a framework.

Many third-party libraries come in a form of Frameworks for macOS (i.e. SDL), while providing an option to be loaded as a regular dynamic library

Linking a Framework

The framework can be linked either via a source code:

  {$linkframework SDL2}

or via a linker instructions. (for FPC each parameter should be prefixed with "-k")

-framework SDL2

(fpc command-line would look like:)

-k-framework -kSDL2

Since the concept of Frameworks exist in macOS only, thus the instructions should be carefully isolated from other systems (as might producer compile time or link time errors)

If the linker results in the error, such as "framework cannot be found". The search path for the framework must be specified (using linker "-F" parameter):

-F /Library/Frameworks

Note:

  • the linker search path will be set to the current system-wide SDK used, as configured by Xcode command-line tools.
  • the third party Frameworks should either be installed, or the path should be explicitly specified via -F parameter.

Distributing Application with a Framework

Once of the beauty of bundles system in the macOS, is that Application hides its all resources necessary within a bundle.

All resources, including Library dependencies, such as third-party Frameworks. This removes the need from the end user to install a third-party Frameworks at their macOS separately.

(NEVER distribute system frameworks with your application!)

The typical placement of a Framework in macOS bundle is under "Frameworks" directory of "Contents"

MyApp.app/
  Contents/
    Frameworks/
      SDL2.Framework
      Else.Framework
      Foo.Framework
    Info.plist
    MacOS/
      myapp
    PkgInfo
    Resources/

(for iOS the placement is similar, but different)

See Also

macOS Dynamic Libraries