package core:dynlib
Overview
Cross-platform loading of shared libraries/DLLs and their symbols.
The behaviour of dynamically loaded libraries is specific to the target platform of the program. For in depth detail on the underlying behaviour please refer to your target platform's documentation.
For a full example, see: core/dynlib/example
Index
Types (1)
Constants (1)
Variables (0)
This section is empty.
Procedures (5)
Procedure Groups (0)
This section is empty.
Types
Library ¶
Library :: distinct rawptr
A handle to a dynamically loaded library.
Related Procedures With Parameters
Constants
LIBRARY_FILE_EXTENSION ¶
LIBRARY_FILE_EXTENSION :: _LIBRARY_FILE_EXTENSION
The file extension for dynamic libraries on the target OS.
Variables
This section is empty.
Procedures
initialize_symbols ¶
initialize_symbols :: proc( symbol_table: ^$T, library_path: string, symbol_prefix: string = "", handle_field_name: string = "__handle", ) -> (count: int = -1, ok: bool = false) {…}
Scans a dynamic library for symbols matching a struct's members, assigning found procedure pointers to the corresponding entry.
Optionally takes a symbol prefix added to the struct's member name to construct the symbol looked up in the library.
Optionally also takes the struct member to assign the library handle to, __handle by default.
This allows using one struct to hold library handles and symbol pointers for more than 1 dynamic library.
A reload loads the new library first and then unloads the previous incarnation, allowing for
straightforward hot reload support. Symbols that only the previous incarnation provided are set to
nil. Reloading the same path has to unload the previous incarnation before loading it again.
Returns:
-1, false if the library could not be loaded, including when that runs out of memory. The
struct and the previous incarnation are left untouched, unless the same path was being
reloaded: then its symbols and the handle are nil.
The number of symbols assigned on success. ok = true if count > 0
See doc.odin for an example.
last_error ¶
last_error :: proc() -> string {…}
Returns an error message for the last failed procedure call.
load_library ¶
load_library :: proc( path: string, global_symbols: bool = false, allocator := context.temp_allocator, ) -> (library: Library, did_load: bool) {…}
Loads a dynamic library from the filesystem. The paramater global_symbols makes the symbols in the loaded
library available to resolve references in subsequently loaded libraries.
The parameter global_symbols is only used for the platforms linux, darwin, freebsd and openbsd.
On windows this paramater is ignored.
The underlying behaviour is platform specific.
On linux, darwin, freebsd and openbsd refer to dlopen.
On windows refer to LoadLibraryW. Also temporarily needs an allocator to convert a string.
path is copied with allocator first; if that runs out of memory, did_load is false and
last_error does not describe it.
Example:
import "core:dynlib"
import "core:fmt"
load_my_library :: proc() {
LIBRARY_PATH :: "my_library.dll"
library, ok := dynlib.load_library(LIBRARY_PATH)
if ! ok {
fmt.eprintln(dynlib.last_error())
return
}
fmt.println("The library %q was successfully loaded", LIBRARY_PATH)
}
symbol_address ¶
symbol_address :: proc( library: Library, symbol: string, allocator := context.temp_allocator, ) -> (ptr: rawptr, found: bool) #optional_ok {…}
Loads the address of a procedure/variable from a dynamic library.
The underlying behaviour is platform specific.
On linux, darwin, freebsd and openbsd refer to dlsym.
On windows refer to GetProcAddress. Also temporarily needs an allocator to convert a string.
symbol is copied with allocator first; if that runs out of memory, found is false and
last_error does not describe it.
Example:
import "core:dynlib"
import "core:fmt"
find_a_in_my_library :: proc() {
LIBRARY_PATH :: "my_library.dll"
library, ok := dynlib.load_library(LIBRARY_PATH)
if ! ok {
fmt.eprintln(dynlib.last_error())
return
}
a, found_a := dynlib.symbol_address(library, "a")
if found_a {
fmt.printf("The symbol %q was found at the address %v", "a", a)
} else {
fmt.eprintln(dynlib.last_error())
}
}
unload_library ¶
Unloads a dynamic library.
The underlying behaviour is platform specific.
On linux, darwin, freebsd and openbsd refer to dlclose.
On windows refer to FreeLibrary.
Example:
import "core:dynlib"
import "core:fmt"
load_then_unload_my_library :: proc() {
LIBRARY_PATH :: "my_library.dll"
library, ok := dynlib.load_library(LIBRARY_PATH)
if ! ok {
fmt.eprintln(dynlib.last_error())
return
}
did_unload := dynlib.unload_library(library)
if ! did_unload {
fmt.eprintln(dynlib.last_error())
return
}
fmt.println("The library %q was successfully unloaded", LIBRARY_PATH)
}
Procedure Groups
This section is empty.
Source Files
Generation Information
Generated with odin version dev-2026-12 (vendor "odin") Windows_amd64 @ 2026-12-03 21:06:39.177575100 +0000 UTC