Sign in Join
Wiki copytemporary

macOS Frameworks: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11A framework is a bundle (a structured directory) that contains a dynamic shared library along with its associated resources, such as image files and header files. When you develop an application, your project links to one or more frameworks. Your code accesses the capabilities of a framework through the application programming interface (API), which is published by the framework through its header files. Because the library is dynamically shared, multiple applications can access the framework code and resources simultaneously. The system loads the code and resources of a framework into memory, as needed, and shares the one copy of a resource among all applications. A special kind of Framework is a system Plugin (or driver).
22
33Distributing dynamic libraries as Frameworks is unique to macOS. All macOS system APIs come in the form of a framework. Many third-party libraries also come in the form of Frameworks for macOS (eg SDL) while providing an option to be loaded as a regular dynamic library.
44
55## Linking to a Framework
66
77The framework can be linked either via a source code:
88
99``` pascal
1010{$linkframework SDL2}
1111```
1212
1313or via a linker instructions:
1414
15-`-framework SDL2`
15+```text
16+-framework SDL2
17+```
1618
1719For FPC command-line, each parameter should be prefixed with "-k":
1820
19-`-k-framework -kSDL2`
21+```text
22+-k-framework -kSDL2
23+```
2024
2125Since the concept of Frameworks exists in macOS only, these instructions should be carefully isolated from other systems (otherwise they might produce compile time or link time errors).
2226
2327If linker produces an error such as "framework cannot be found", the search path for the framework must be specified (using the "-F" linker parameter):
2428
25-`-F /Library/Frameworks`
29+```text
30+-F /Library/Frameworks
31+```
2632
2733**Note**:
2834
2935- the linker search path will be set to the current system-wide SDK used, as configured by Xcode command-line tools.
3036- any third party Frameworks should either be installed, or the path should be explicitly specified via -F parameter.
3137- -F is only used for the developer machine at link-time. It doesn't affect how the framework is searched at the run-time
3238
3339## Distributing an application with a Framework
3440
3541![](/wiki-images/3/35/Frameworkconfigureoption.png)
3642
3743One of the beauties of the **bundles** system in macOS is that the application hides all of its necessary resources within a [[Application Bundle|bundle]]. Resources include library dependencies, such as third-party frameworks, which remove the need for the end user to have to install separately any needed third-party framework in their macOS system.
3844
3945(NEVER distribute system frameworks with your application!)
4046
4147The typical placement of a framework in **macOS** bundle is under "Frameworks" directory of "Contents":
4248
43-`MyApp.app/`\
44-`  Contents/`\
45-`    Frameworks/`\
46-`      SDL2.Framework`\
47-`      Else.Framework`\
48-`      Foo.Framework`\
49-`    Info.plist`\
50-`    MacOS/`\
51-`      myapp`\
52-`    PkgInfo`\
53-`    Resources/`
49+```text
50+MyApp.app/
51+ Contents/
52+ Frameworks/
53+ SDL2.Framework
54+ Else.Framework
55+ Foo.Framework
56+ Info.plist
57+ MacOS/
58+ myapp
59+ PkgInfo
60+ Resources/
61+```
5462
5563(for iOS the placement is similar, but different)
5664
5765However, such framework will need to be loaded in runtime. Thus the linker MUST BE given an instruction to specify the "Frameworks" folder as a runtime framework load path.
5866
5967This is done by specifying additional linker command:
6068
61-`-rpath @loader_path/../Frameworks`
69+```text
70+-rpath @loader_path/../Frameworks
71+```
6272
6373- "@loader_path" - is a special value that is recognized by the dynamic loader (see "man dyld") during the application load. For the macOS application bundle it would be the "MacOS" directory. As a result "@loader_path../Frameworks" will actually point to "Frameworks" folder of the bundle.
6474
6575### Troubleshooting
6676
6777If "rpath" was not specified, the application might fail to start. The error log (or running from the command line) might reveal an error like this:
6878
69-`user@user-mac MacOS % ./myapp `\
70-` dyld: Library not loaded: @rpath/SDL2.framework/Versions/A/SDL2_mixer`\
71-`    Referenced from: /Applications/MyApp.app/Contents/MacOS/./myapp`\
72-` Reason: image not found`\
73-`zsh: abort      ./myapp`
79+```text
80+user@user-mac MacOS % ./myapp
81+ dyld: Library not loaded: @rpath/SDL2.framework/Versions/A/SDL2_mixer
82+ Referenced from: /Applications/MyApp.app/Contents/MacOS/./myapp
83+ Reason: image not found
84+zsh: abort ./myapp
85+```
7486
7587In order to see what RPATH was specified by the linker, simply run the `otool -l myapp` command. At the end of the load commands list you should see an "LC_RPATH" entry (or entries):
7688
77-`Load command 28`\
78-`        cmd LC_RPATH`\
79-`    cmdsize 40`\
80-`       path @loader_path/../Frameworks (offset 12)`
89+```text
90+Load command 28
91+ cmd LC_RPATH
92+ cmdsize 40
93+ path @loader_path/../Frameworks (offset 12)
94+```
8195
8296If you see no LC_RPATH then none was specified.
8397
8498## Frameworks or Dynamic Libraries
8599
86100Frameworks themselves are simply special directory structures that contain a dynamic library, and possibly headers and other associated resources. The same approach as application [[Application Bundle|bundles]].
87101
88102In many cases it's possible to load a dynamic library that resides within a framework structure at run-time, without needing to link the framework.
89103
90104There's no restriction so far NOT to use dynamic libraries. The choice should be driven by the code maintenance tasks. Linking frameworks might be easier when it comes to an update (as there's no need to track the version of the library, if it needs to change).
91105
92106## See also
93107
94108- [[Application Bundle]].
95109- [[macOS Dynamic Libraries]].
96110- [[macOS Static Libraries]].
97111- [[macOS Programming Tips|macOS Libraries]].
98112
99113## External links
100114
101115- [Apple: Framework Programming Guide](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Frameworks.html#//apple_ref/doc/uid/10000183i).
102116
103117---
104118*From the Free Pascal wiki, page "macOS Frameworks", as it stood on 2021-12-06 after a change by Alextp, 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](/wiki-contact).*

Back to history