Hi guys, after reading this thread <https://kotlinlang.slack.com/archives/C3SGXARS6/p167283444124609...
p
Hi guys, after reading this thread https://kotlinlang.slack.com/archives/C3SGXARS6/p1672834441246099 , I find out a good way to ship my prebuilt static libraries with klib, thank @kpgalligan for sharing these two undocumented flags. Actually it’s quit simple, when adding a native target, e.g. linuxX64, in groovy:
Copy code
kotlin {
  linuxX64 {
    compilations.main {
      cinterops {
        xlog {
          defFile "src/cppCommon/cinterop/xlog.def"
          includeDirs {
            allHeaders "${project.projectDir}/src/cppCommon/cpp"
          }
        }
      }

      kotlinOptions.freeCompilerArgs += [
          "-include-binary",
          "${project.projectDir}/src/linuxMain/libs/x64/libkmp_xlog.a".toString()
      ]
    }
  }
}
The key point is setting
kotlinOptions.freeCompilerArgs
. I’m wondering another thing: my c++ code depends on
pthread
and
z
, so in module which produces executable, my lib users need to add these two linker flags,
Copy code
kotlin {
  linuxX64 {
    binaries {
      all {
        linkerOpts = [
          "-lpthread",
          "-lz",
          ]
      }

      executable("kmp_xlog") {
        entryPoint = "com.piasy.kmp.xlog.example.main"
      }
    }
  }
}
Is it possible that I specify these two linker flags in my library module, so they can be added to dependent modules automatically? So my lib users only need to add a gradle dependency, no need to set linkerOpts.
v
Don't the linker flags from your def files apply automatically when depending on them from a client module?
p
Oh that would be nice! I’ll try it.
Thank you!
v
I haven't done that much with it yet, but IIRC I never had to add specific linker flags when using my GTK modules. the def files in my GTK hacking libs have all the required linker flags and they seem to get applied automatically.
From my testing it works like that when depending on a library from within the same multi-module project, and from mavenLocal. I haven't tested my own libs with an actual published repository yet, but from some limited testing with Sqliter/Sqldelight it seems to be the case as well.
p
Yes, it also works in my case, I’m depending my lib from maven local, and it should be the same for maven local and maven central.