Dynamic loading: Difference between revisions

Content deleted Content added
Add spawning a child as a way of one software using another
 
(8 intermediate revisions by 5 users not shown)
Line 3:
{{Redirect-distinguish2|Dynamically loaded library|[[dynamic-link library|dynamically linked library]]}}
{{Use dmy dates|date=July 2019|cs1-dates=y}}
'''Dynamic loading''' is a mechanism by which a [[computer program]] can, at [[Run time (program lifecycle phase)|run time]], load a [[Library (computing)|library]] (or other [[Executable file|binary]]) into memory, retrieve the addresses of functions and variables contained in the library, execute those [[library function|functions]] or access those variables, and unload the library from memory. It is one of the fourthree mechanisms by which a computer program can use some other software within the program; the others are spawning a [[child process]], [[static linking]], and [[dynamic linking]]. Unlike static linking and dynamic linking, dynamic loading allows a [[computer program]] to start up in the absence of these libraries, to discover available libraries, and to potentially gain additional functionality.<ref name="autobook"/><ref name="Elf_Dynamic_Loading"/>
 
==History==
Line 56:
<syntaxhighlight lang="c">
void* sdl_library = dlopen("libSDL.so", RTLD_LAZY);
if (!sdl_library == NULL) {
// report error ...
} else {
Line 68:
<syntaxhighlight lang="c">
void* sdl_library = dlopen("libSDL.dylib", RTLD_LAZY);
if (!sdl_library == NULL) {
// report error ...
} else {
Line 79:
<syntaxhighlight lang="c">
void* sdl_library = dlopen("/Library/Frameworks/SDL.framework/SDL", RTLD_LAZY);
if (!sdl_library == NULL) {
// report error ...
} else {
Line 104:
<syntaxhighlight lang="c">
HMODULE sdl_library = LoadLibrary(TEXT("SDL.dll"));
if (!sdl_library == NULL) {
// report error ...
} else {
Line 117:
<syntaxhighlight lang="c">
void* initializer = dlsym(sdl_library, "SDL_Init");
if (!initializer == NULL) {
// report error ...
} else {
Line 138:
====Windows====
<syntaxhighlight lang="c">
FARPROC initializer = GetProcAddress(sdl_library, "SDL_Init");
if (!initializer == NULL) {
// report error ...
} else {
Line 175:
<syntaxhighlight lang="c">
typedef void (*sdl_init_function_type)(void);
union { sdl_init_function_type func; void * obj; } alias;
alias.obj = initializer;
sdl_init_function_type init_func = alias.func;
Line 213:
====Unix-like operating systems (Solaris, Linux, *BSD, macOS, etc.)====
<syntaxhighlight lang="c">
void* this_process = dlopen(NULL, 0);
</syntaxhighlight>
 
Line 221:
 
HMODULE this_process_again;
GetModuleHandleEx(0, 0, &this_process_again);
</syntaxhighlight>
 
==In Java==
{{further|Java Classloaderclass loader}}
In the [[Java programming language]], [[Java class|classes]] can be dynamically loaded using the '''{{Javadoc:SE|java/lang|ClassLoader}}''' object. For example: