Having declared the external function or variable that resides in an object file, you can use it as if it was defined in your own program or unit. To produce an executable, you must still link the object file in. This can be done with the {$L file.o} directive.
This will cause the linker to link in the object file file.o. On most systems, this filename is case sensitive. The object file is first searched in the current directory, and then the directories specified by the -Fo command line.
You cannot specify libraries in this way, it is for object files only.
Here we present an example. Consider that you have some assembly routine which uses the C calling convention that calculates the nth Fibonacci number:
Then you can call this function with the following Pascal Program:
With just two commands, this can be made into a program:
This example supposes that you have your assembler routine in fib.s, and your Pascal program in fibo.pp.
To link your program to a library, the procedure depends on how you declared the external procedure.
In case you used the following syntax to declare your procedure:
You don’t need to take additional steps to link your file in, the compiler will do all that is needed for you. On Windows it will link to name.dll, on linux and most unix’es your program will be linked to library libname, which can be a static or dynamic library.
In case you used
You still need to explicitly link to the library. This can be done in 2 ways:
This will link to the gpm library. On unix systems (such as linux), you must not specify the extension or ’lib’ prefix of the library. The compiler takes care of that. On other systems (such as Windows), you need to specify the full name.
Is equivalent to the above method, and tells the linker to link to the gpm library.
As an example, consider the following program:
This program can be compiled with:
Supposing, of course, that the program source resides in prlen.pp.
To use functions in C that have a variable number of arguments, you must compile your unit or program in objfpc mode or Delphi mode, and use the Array of const argument, as in the following example:
The output of this program looks like this:
As an alternative, the program can be constructed as follows:
The varargs modifier signals the compiler that the function allows a variable number of arguments (the ellipsis notation in C).