v / examples / dynamic_library_loading
Raw file | 19 loc (16 sloc) | 563 bytes | Latest commit hash 017ace6ea
1module main
2
3// Note: This program, requires that the shared library was already compiled.
4// To do so, run `v -d no_backtrace -o library -shared modules/library/library.v`
5// before running this program.
6import os
7import dl
8
9type FNAdder = fn (int, int) int
10
11fn main() {
12 library_file_path := os.join_path(os.dir(@FILE), dl.get_libname('library'))
13 handle := dl.open_opt(library_file_path, dl.rtld_lazy)!
14 eprintln('handle: ${ptr_str(handle)}')
15 f := FNAdder(dl.sym_opt(handle, 'add_1')!)
16 eprintln('f: ${ptr_str(f)}')
17 res := f(1, 2)
18 eprintln('res: ${res}')
19}