v / vlib / dl
Raw file | 48 loc (41 sloc) | 1.2 KB | Latest commit hash 017ace6ea
1module dl
2
3pub const version = 1
4
5pub const dl_ext = get_shared_library_extension()
6
7pub const rtld_next = voidptr(-1)
8
9// get_shared_library_extension returns the platform dependent shared library extension
10// i.e. .dll on windows, .so on most unixes, .dylib on macos.
11[inline]
12pub fn get_shared_library_extension() string {
13 return $if windows {
14 '.dll'
15 } $else $if macos {
16 '.dylib'
17 } $else {
18 '.so'
19 }
20}
21
22// get_libname returns a library name with the operating system specific extension for
23// shared libraries.
24[inline]
25pub fn get_libname(libname string) string {
26 return '${libname}${dl.dl_ext}'
27}
28
29// open_opt tries to load a given dynamic shared object.
30pub fn open_opt(filename string, flags int) !voidptr {
31 shared_object_handle := open(filename, flags)
32 if shared_object_handle == 0 {
33 e := dlerror()
34 return error(e)
35 }
36 return shared_object_handle
37}
38
39// sym_opt returns the address of a symbol in a given shared object, if found.
40// Unlike sym, sym_opt returns an option.
41pub fn sym_opt(shared_object_handle voidptr, symbol string) !voidptr {
42 sym_handle := sym(shared_object_handle, symbol)
43 if sym_handle == 0 {
44 e := dlerror()
45 return error(e)
46 }
47 return sym_handle
48}