macOS Dynamic Libraries

From Free Pascal wiki
Revision as of 14:30, 13 February 2020 by Trev (talk | contribs) (New page on macOS dynamic libraries)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Overview

There are two important factors which determine the performance of applications: their launch times and their memory footprints. Reducing the size of an executable file and minimizing its memory use once launched make an application launch faster and use less memory. Using dynamic libraries instead of static libraries reduces the executable file size of an application. Dynamic libraries also allow applications to delay loading libraries with special functionality until they’re needed instead of loading them at launch time. This feature contributes further to reduced launch times and efficient memory use.

Static vs Dynamic

Most of an application's functionality is implemented in libraries of executable code. When an application's source code is compiled into object code and linked with a static library, the object code and library code that the application uses is copied into the executable file that is loaded into memory in its entirety at runtime. The kind of library that becomes part of an application's executable is known as a static library. Static libraries are collections or archives of object files.

A better approach is for an application to load code into its address space when it’s actually needed, either at launch time or at runtime. The type of library that provides this flexibility is called a dynamic library. Dynamic libraries are not statically linked into the executable and therefore do not become part of the executable. Instead, dynamic libraries can be loaded (and linked) into an application either when the application is launched or as it runs.

Example dynamic library

test.pas:

library TestLibrary;
  
{$mode objfpc} {$H+}

uses
  // needed for UpperCase
  SysUtils;     

// library subroutine
function cvtString(strIn : string) : PChar; cdecl;
  begin
    cvtString := PChar(UpperCase(strIn));
  end;

// exported subroutine(s)
exports
  cvtString;
end.

Compile:

 fpc test.pas

which produces the dynamic library file named libtest.dylib.

Example application to load dynamic library

dynlibdemo.pas:

Program dynlibdemo;
  
{$mode objfpc}{$H+}

uses
   Dynlibs,
   SysUtils;

type
   // definition of the subroutine to be called as defined in the dynamic library to be loaded
   TcvtString = function(strToConvert : string) : PChar;  cdecl;

var
   // create suitable variable for the dynamic library subroutine
   cvtString : TcvtString;

   // create a handle for the dynamic library
   LibHandle : TLibHandle;

begin
   // load and get the dynamic library handle
   LibHandle := LoadLibrary(PChar('libtest.dylib'));

   // check whether loading was successful
   if LibHandle <> 0 then
     begin
       // assign address of the subroutine call to the variable cvtString
       Pointer(cvtString) := GetProcAddress(LibHandle, 'cvtString');

       // check whether a valid address has been returned
       if @cvtString <> nil then
         WriteLn(cvtString('hello world'))
       // error message on no valid address
       else
         WriteLn('GetLastOSError = ', SysErrorMessage(GetLastOSError));
     end
   else
     // error message on load failure
     WriteLn('GetLastOSError = ', SysErrorMessage(GetLastOSError));

   // release memory
   cvtString := nil;
   FreeLibrary(LibHandle);
end.

Compile:

 fpc dynlibdemo.pas

Run:

$ ./dynlibdemo
HELLO WORLD

See also