1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vlib: add a new dl.loader module, to simplify dynamic library loading, when the DLLs may be in multiple customisable locations (#17161)

This commit is contained in:
Ulises Jeremias Cornejo Fandos
2023-01-31 04:27:48 -03:00
committed by GitHub
parent 2d51379f00
commit 40ec2a292e
7 changed files with 397 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
module main
// Note: This program, requires that the shared library was already compiled.
// To do so, run `v -d no_backtrace -o library -shared modules/library/library.v`
// before running this program.
import os
import dl
import dl.loader
type FNAdder = fn (int, int) int
const (
cfolder = os.dir(@FILE)
default_paths = [
os.join_path(cfolder, 'library${dl.dl_ext}'),
os.join_path(cfolder, 'location1/library${dl.dl_ext}'),
os.join_path(cfolder, 'location2/library${dl.dl_ext}'),
os.join_path(cfolder, 'modules/library/library${dl.dl_ext}'),
]
)
fn main() {
mut dl_loader := loader.get_or_create_dynamic_lib_loader(
key: cfolder + '/library'
paths: default_paths
)!
defer {
dl_loader.unregister()
}
sym := dl_loader.get_sym('add_1')!
f := FNAdder(sym)
eprintln('f: ${ptr_str(f)}')
res := f(1, 2)
eprintln('res: ${res}')
}