next up previous contents
Next: Objects Up: Linking issues Previous: Linking your program to

Making a shared library

 

Free Pascal supports making shared libraries in a straightforward and easy manner. If you want to make libraries for other Free Pascal programmers, you just need to provide a command line switch. If you want C programmers to be able to use your code as well, you will need to adapt your code a little. This process is described first.

Adapting your code

If you want to make your procedures and functions available to C programmers, you can do this very easily. All you need to do is declare the functions and procedures that you want to make available as Export, as follows:

Procedure ExportedProcedure ; export;
This tells the compiler that it shouldn't clear the stack upon exiting the procedure (see section (3.3)), thus enabling a C program to call your function. It also means that your Pascal program can't call this function, since it will be using the C calling mechanism.

Remark : You can only declare a function as exported in the Implementation section of a unit. This function may not appear in the interface part of a unit. This is logical, since a Pascal routine cannot call an exported function, anyway.

However, the generated object file will not contain the name of the function as you declared it. The Free Pascal compiler ''mangles'' the name you give your function. It makes the name all-uppercase, and adds the types of all parameters to it. For Free Pascal units, this doesn't matter, since the .ppu unit file contains all information to map your function declaration onto the mangled name in the object file. For a C programmer, who has no access to the .ppu file, this is not very convenient. That is why Free Pascal has the Alias modifier. The Alias modifier allows you to specify another name (a nickname) for your function or procedure.

The prototype for an aliased function or procedure is as follows :

Procedure AliasedProc; [ Alias : 'AliasName'];
The procedure AliasedProc will also be known as AliasName. Take care, the name you specify is case sensitive (as C is).

Of course, you want to combine these two features of Free Pascal, to export a function under a reasonable name; If you want to do that, you must first specify that the function is to be exported, and then only declare an alias:

Procedure ExportToCProc; Export; [Alias : 'procname'];
After that, any C program will be able to use your procedure or function.

Remark: If you use in your unit functions that are in other units, or system functions, then the C program will need to link in the object files from the units too.

Compiling libraries

Once you have your (adapted) code, with exported and other functions, you can compile your unit, and tell the compiler to make it into a library. The compiler will simply compile your unit, and perform the necessary steps to transform it into a static or shared (dynamical) library.

You can do this as follows, for a dynamical library:

ppc386 -Uld myunit
On LINUX this will leave you with a file libmyunit.so. On and OS/2, this will leave you with myunit.dll.

If you want a static library, you can do

ppc386 -Uls myunit
This will leave you with libmyunit.a and a file myunit.ppl. The myunit.ppl is the unit file needed by the Free Pascal compiler. The extension .ppl means that the file describes a unit that resides in a library.

The resulting files are then libraries. To make static libraries, you need the ranlib or ar program on your system. It is standard on any LINUX system, and is provided with the GCC compiler under DOS.

BEWARE: This command doesn't include anything but the current unit in thelibrary. Other units are left out, so if you use code from other units, you must dpley them together with your library.

Moving units into a library

You can put multiple units into a library with the ppumove command, as follows:

ppumove unit1 unit2 unit3 name
This will move 3 units in 1 library (called libname.so on linux, name.dll on ) and it will create 3 files unit1.ppl, unit2.ppl and file3.ppl, which are unit files, but which tell the compiler to look in library name when linking your executable.

The ppumove program has options to create statical or dynammical libraries. It is provided with the compiler.

Unit searching strategy

When you compile a program or unit, the compiler will by default always look for .ppl files. If it doesn't find one, it will look for a .ppu file. You can disable this behaviour by specifying the -Cs switch. This tells the compiler to make a static binary, and refrains it from looking for units which reside in a library.

You can tell the compiler only to use dynamic libraries by specifying the -Cd switch; the compiler will then only look for .ppl files, and will give an error if it doesn't find one.


next up previous contents
Next: Objects Up: Linking issues Previous: Linking your program to

Michael Van Canneyt
Tue Mar 31 16:50:06 CEST 1998