Jeff Lockhart
09/30/2022, 2:08 AMTijl
09/30/2022, 8:07 AMlinkerOpt
with -rpath
?Dominaezzz
09/30/2022, 8:17 AMJeff Lockhart
09/30/2022, 5:15 PM-rpath
to reference the relative path at runtime, but was still figuring out how to pass the linker option in gradle and just realized I can do it in the same place! 🤦♂️ (I was figuring out -rpath
after giving up on gradle linkerOpts()
not working for -L
.)
For context, if anyone else runs into this, I'm adding Linux and Windows native targets to my library. I'm able to reference the headers with a relative path from gradle with includeDirs()
, but linkerOpts()
isn't supported by cinterop:
warning: -linker-option(s)/-linkerOpts option is not supported by cinterop. Please add linker options to .def file or binary compilation instead.But .def doesn't support relative paths and somehow I overlooked the binary compilation mentioned at the end. I was able to link the mingwX64 library's relative path, since it uses a .lib import library which is statically linked. But this doesn't work for the Linux .so. Instead of passing
linkerOpts()
to cinterop, I need to pass it to the test binary:
build.gradle.kts
kotlin {
...
targets.withType<KotlinNativeTarget> {
val libPath = "$projectDir/<lib-relative-path>"
val main by compilations.getting
val libName by main.cinterops.creating {
includeDirs("<lib-headers-relative-path>")
if (konanTarget.family == Family.MINGW) {
extraOpts("-libraryPath", libPath)
}
//linkerOpts(...) <- does not work!
}
if (konanTarget.family == Family.LINUX) {
binaries.getTest(DEBUG).linkerOpts += listOf(
"-L$libPath", "-llibName", "-rpath", libPath
)
}
}
}
libName.def
...
staticLibraries.mingw_x64 = libName.lib
I also need to use a copy task to copy the dynamic .dll to the .kexe directory to find the library at runtime, since Windows doesn't support -rpath
.黄文和
10/31/2022, 10:02 AM