Is there a way to handle `symbol lookup error` at ...
# kotlin-native
r
Is there a way to handle
symbol lookup error
at runtime? Like, try/catch or something similar these errors?
Copy code
debugExecutable/app.kexe: symbol lookup error: debugExecutable/app.kexe: undefined symbol: gdk_cicp_params_get_type
e
use dlsym
r
@ephemient thanks a lot! I did some quick tests and this seems to work on both linux and mac!
Copy code
fun getTypeOrNull(symbolName: String): GType? {
    // Passing null (or "") tells dlopen to search within the main program or any already-loaded shared libraries
    val handle = dlopen(null, RTLD_LAZY) ?: return null
    val symbolPtr = dlsym(handle, symbolName)
    if (symbolPtr == null) {
        dlclose(handle)
        return null
    }
    val function = symbolPtr.reinterpret<CFunction<() -> GType>>()
    val result: GType = function.invoke()
    dlclose(handle)
    return result
}