v/vlib/dl/dl_nix.c.v

43 lines
1009 B
V
Raw Permalink Normal View History

2020-04-28 11:53:55 +02:00
module dl
#include <dlfcn.h>
2021-02-23 08:46:28 +01:00
$if linux {
#flag -ldl
}
pub const rtld_now = C.RTLD_NOW
pub const rtld_lazy = C.RTLD_LAZY
2020-04-28 11:53:55 +02:00
fn C.dlopen(filename &char, flags int) voidptr
2020-04-29 21:01:19 +02:00
fn C.dlsym(handle voidptr, symbol &char) voidptr
2020-04-29 21:01:19 +02:00
fn C.dlclose(handle voidptr) int
fn C.dlerror() &char
2020-04-29 21:01:19 +02:00
// open loads the dynamic shared object.
2020-04-28 11:53:55 +02:00
pub fn open(filename string, flags int) voidptr {
return C.dlopen(&char(filename.str), flags)
2020-04-28 11:53:55 +02:00
}
2020-04-29 21:01:19 +02:00
// close frees a given shared object.
pub fn close(handle voidptr) bool {
return C.dlclose(handle) == 0
}
// sym returns an address of a symbol in a given shared object.
2020-04-28 11:53:55 +02:00
pub fn sym(handle voidptr, symbol string) voidptr {
return C.dlsym(handle, &char(symbol.str))
2020-04-28 11:53:55 +02:00
}
// dlerror provides a text error diagnostic message for functions in `dl`
// it returns a human-readable string, describing the most recent error
// that occurred from a call to one of the `dl` functions, since the last
// call to dlerror()
pub fn dlerror() string {
return unsafe { cstring_to_vstring(C.dlerror()) }
}