I’m building an executable that runs on MacOS. I’m...
# kotlin-native
s
I’m building an executable that runs on MacOS. I’m using a dynamic library and no matter what I do, it’s looking for that library at
/usr/local/lib/
1. How do I tell get the dynamic library to load a file in a relative path to the
.kexe
executable? 2. If #1 isn’t possible, is it possible to embed a dynamic library into the
.kexe
?
a
Yesterday I wrote some code that exactly deals with "where am I?" (kexe) problem, and wrote some code like this (they most unlikely work on osx and windows though) https://github.com/atsushieno/mugene-ng/commit/d6efe802
s
@atsushieno I’m new to native programming so I don’t understand how this solves the problem for me. My application won’t open to run any of my code, because it can’t find the dynamic library. The code you link looks like it would allow me to get the running directory once my application has opened.
a
Ah, I thought the question (especially 1.) is about resolving a file path from the executable. Loading a library could be achieved by
dlopen()
function.
My code ^ is to get the executable path which was not surprisingly simple (for me).
s
Oh - How would I load the file with dlopen and not link the file at compilation?
i.e. if I remove this line from my build script
Copy code
linkerOpts(project.file("libs/<dynamic lib>.dylib").absolutePath)
It no longer builds
I get several
Copy code
Undefined symbols for architecture x86_64:
a
I assume you have some explicit reference to the library somewhere in your code. When you load a library using
dlopen()
, then you are supposed to get a pointer to a symbol (function or variable) using
dlsym()
and optionally cast to your favorite type.
"explicit reference to the library" or more precisely, explicit reference to the function (or variable) name in the library.
s
Is there an example of this happening in Kotlin?
I found an example. Using a library in this manner seems a little miserable. I’d have to give up any benefits of static typing. - https://gist.github.com/tailriver/30bf0c943325330b7b6a