1、Dynamic library developers can set a different install name for a library when they compile it using the gcc -install_name option.
2、use
dynamic loader compatibility (DLC)
functions.
3、Dependent libraries are loaded into the same process the client is being loaded into as part of its load process. For example, when an app is launched, its dependent libraries are loaded as part of the launch process, before the main function is executed.
Runtime loaded library is a dynamic library the client opens with the dlopen
注:两种情况下不能解析的符号都被链接为0
在动态库中打开其自身的dependent库不用dlopen,直接dlsym即可,因为其dependent在其加载时也一起被加载了。
在client中非dependent库必须dlopen才能用,即使是其dependent的dependent也不行
4、
Minor revisions to the library are released under the same filename used by the previous major revision. while
filenames of subsequent major revisions of the library have different (and preferably incremental) major version numbers.
The compatibility version number of a library release specifies the earliest minor version of the clients linked with that release can use.
To set the minor version number of a dynamic library, use the clang -current_version <version_number> option.
-compatibility_version
加载条件:current_version >= compatibility_version
To view a library’s current and compatibility versions, use the
otool -L <library>
command.
After you compile your library, you can view its dependent libraries in a shell editor with the
otool -L
command.
Thedynamicloaderperformstheversioncompatibilitytestonlywithdependentlibraries. Dynamic libraries opened at runtime with
dlopen
don’t go through this test.
5、
Global variables should never be exported.
If clients need to access a value stored in a global variable, your library should export accessor functions but not the global variable itself.
6、
With weak linking, clients do not fail to launch when the version of the dependent library found at launch time or load time doesn’t export a weakly linked symbol referenced by the client.
The compiler -weak_library command-line option: This option tells the compiler to treat all the library’s exported symbols as weakly linked symbols.
7、hide the internal interface from client
- static, but the static storage class is not appropriate to hide symbols from the client but disclose them to all the library’s modules.
- export_list, symbol前加_
- set the visibility attribute in their implementations to "default" and set the -fvisibility compiler command-line option to hidden when compiling the library’s source files.
-
/* File: Person.c */
#include "Person.h" #include <string.h>
// Symbolic name for visibility("default") attribute. #define EXPORT __attribute__((visibility("default"))) // 在源文件使用
char _person_name[30] = {'\0'};
EXPORT // Symbol to export char* name(void) {
return _person_name; }
void _set_name(char* name) {
strcpy(_person_name, name);
}
EXPORT // Symbol to export void set_name(char* name) {
if (name == NULL) {
_set_name("");
}
else
{
_set_name(name);
}
}
The library would then be compiled using the following command:
% clang -dynamiclib Person.c -fvisibility=hidden -o libPerson.dylib
8、
Locating External Resources
- Executable-relative location, @executable_path
- Library-relative location, @loader_path
9、
Initializers and finalizers must not be exported.因此必须用static修饰
10、
Defining C++ Class Interfaces
dependent library
:clients must have access to a class’s constructors and destructors so that they can use the
new
and
delete
operators on the class
.
R
untime loaded library:
clients that open a C++–based library at runtime through dlopen do not have access to the library’s constructors because the constructors are exported with their names mangled, preventing the dynamic loader from locating them
其它的成员函数要公开接口,必须用虚函数
11、
Objective-C–based dynamic libraries provide several initialization facilities for modules, classes, and categories:
-
The +load method: Initializes resources needed by a class or a category. The Objective-C runtime sends the load message to every class a library implements; it then sends the load message to every category the library implements.
-
Module initializers: Initializes a module. The
dynamic loader calls
all the initializer functions (defined with the
constructor
attribute) in each of a library’s modules.
dependent library
在main之前加载,故在main之前调用initializer,并在main结束之后调用finitializer
runtime load library在main之后加载,故在main之后调用initializer,且
在main结束之后调用finitializer
-
The +initialize method:
-
The Objective-C runtime sends the initialize message to a class just before creating an instance of the class.
12、
Creating Aliases to a Class---how????
13、
The dynamic loader loads an image’s dependent libraries when the image is opened; that is, when an app is loaded or when a dynamic library is opened. The dynamic loader binds references to symbols exported by dependent libraries lazily. Lazy binding means that the symbol references are bound only when the image actually uses the symbols.
As a debugging measure
use the compiler
-bind_at_load
command-line option when generating the dynamic library.
The external undefined symbols in dependent libraries are bound when they are first used unless the client image’s compile line includes the
-bind_at_load
option. See the
ld
man page for details.
14、Scope
T
he symbols in the global scope are available to all images in the process, including other dynamically loaded libraries. Symbols in the local scope can be used only by the image that opened the library.
Allruntimeloadeddynamiclibrariesshouldbeopenedintothelocalscope.
15、
Using Weakly Linked Symbols
/* File: Person.h */
#define WEAK_IMPORT __attribute__((weak_import)) // 在头文件中使用
char* name(void);
void set_name(char* name);
WEAK_IMPORT void clear_name(void);
The dependent library loads successfully whether or not it implements clear_name. But it doesn’t load if it doesn’t define either name or set_name.
16、
The standard locations for header files are ~/include, /usr/local/include and /usr/include. The standard locations for dynamic libraries are ~/lib, /usr/local/lib, and /usr/lib
dynamic labrary 非标准路径安装相关环境变量
LD_LIBRARY_PATH
DYLD_LIBRARY_PATH
DYLD_FALLBACK_LIBRARY_PATH
17、Environment variables that effect dynamic loader logging
Environment variable | Description |
DYLD_PRINT_LIBRARIES
| Logs when images are loaded. |
DYLD_PRINT_LIBRARIES_POST_- LAUNCH | Logs when images are loaded as a result of a dlopen call. Includes a dynamic libraries’ dependent libraries. |
DYLD_PRINT_APIS
| Logs the invocation that causes the dynamic loader to return the address of a symbol. |
DYLD_PRINT_STATISTICS
| Logs statistical information on an application’s launch process, such as how many images were loaded, when the application finishes launching. |
DYLD_PRINT_INITIALIZERS
| Logs when the dynamic loader calls initializer and finalizer functions. |
DYLD_PRINT_SEGMENTS
| Logs when the dynamic loader maps a segment of a dynamic library to the current process’s address space. |
DYLD_PRINT_BINDINGS
| Logs when the dynamic loader binds an undefined external symbol with its definition. |