Originale-mail to me for new edition

 

Calling dynamically loadable libraries

 

You can call operating system routines directly, but they are not linked to your application until runtime. This means that the library need not be present when you compile your program. It also means that there is no compile-time validation of attempts to import a routine.

Before you can call routines defined in a shared object, you must import them. This can be done in two ways: by declaring an external procedure or function, or by direct calls to the operating system. Whichever method you use, the routines are not linked to your application until runtime.

Object Pascal does not support importing of variables from shared libraries.

 

Static loading

The simplest way to import a procedure or function is to declare it using the external directive. For example,

On Windows:procedure DoSomething; external 'MYLIB.DLL';

On Linux:  procedure DoSomething; external 'mylib.so';

If you include this declaration in a program, MYLIB.DLL (Windows) or mylib.so (Linux) is loaded once, when the program starts. Throughout execution of the program, the identifier DoSomething always refers to the same entry point in the same shared library.

Declarations of imported routines can be placed directly in the program or unit where they are called. To simplify maintenance, however, you can collect external declarations into a separate import unit that also contains any constants and types required for interfacing with the library. Other modules that use the import unit can call any routines declared in it.

For more information about external declarations, see External declarations.

 

Topic groups

 

See also

Libraries and packages: Overview

 

 

译文

 

调用动态可加载库

 

可以直接调用操作系统例程,但它们直到运行时才被连接到应用程序。这意味着编译程序时所需的库不必存在。也就是说,编译时不会确认尝试从库中引入例程。

在可以调用定义在共享对象中的例程之前,必须在程序中引入它们。有两种途径可以实现引入:声明外部(external)过程或函数,或者直接调用操作系统。无论通过哪一种途径引入,例程直到运行时才被连接到应用程序。

Object Pascal不支持从共享库中引入变量。

 

静态加载

引入过程或函数最简单的方法是,使用external指示字将其声明为外部例程。例如,

On Windows:procedure DoSomething; external 'MYLIB.DLL';

On Linux:  procedure DoSomething; external 'mylib.so';

如果在程序中包括了上面的声明,那么程序启动时MYLIB.DLLWindows)或mylib.soLinux)被加载一次。在程序执行的全过程中,标识符DoSomething总是指向同一个共享库中相同的入口点。

引入的例程声明可以直接放置在其被调用的程序或单元中。不过,为了简化维护,可以将外部(external)声明收集在一个单独的引入单元,该单元还包括对库请求接口时的所有常量和类型。如此以来,使用了引入单元的其他模块就可以调用声明在该单元中的任何例程。

关于外部(external)声明的更多信息,见外部声明

 

主题组

 

相关主题

库和包:概述