# How to determine linkerOpts at build-time?

**URL:** <https://discuss.kotlinlang.org/t/how-to-determine-linkeropts-at-build-time/17402>\
**Category:** Native\
**Created:** [April 29, 2020, 11:02am UTC](https://discuss.kotlinlang.org/t/how-to-determine-linkeropts-at-build-time/17402 "2020-04-29T11:02:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loke](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Loke](https://discuss.kotlinlang.org/u/Loke)\
**Post date:** [April 29, 2020, 11:02am UTC](https://discuss.kotlinlang.org/t/how-to-determine-linkeropts-at-build-time/17402/1 "2020-04-29T11:02:50Z")

</div>

I have a Kotlin multiplatform project where I link with ICU. To make that work, I have created the following .def file:

```
headers = unicode/ustring.h unicode/utf8.h unicode/uchar.h unicode/ucnv.h unicode/ubrk.h
headerFilter = unicode/*

compilerOpts.linux = -I/usr/include -I/usr/include/x86_64-linux-gnu
linkerOpts.osx = -licui18n -licuuc -licudata
linkerOpts.linux = -L/usr/lib64 -licui18n -licuuc -licudata

```

This works fine on my Fedora system, but when building on Ubuntu this fails because `linkerOpts.linux` is wrong. I need to change the link path to: `/usr/lib/x86_64-linux-gnu`.

This leads me to two questions:

1. Why do I have to specify the link path in the first place? Normally this is determined automatically, and different distributions use different paths. In fact, it’s impossible to predict which ones are needed.
2. Assuming there are some technical reasons why the link path can’t be set up automatically, how can I add some code that runs during build that computes this path automatically? Basically, I’d need to call out to some code that looks at `ld.so.conf` to determine which path should be used.

What is the proper way to solve this problem?

---

<div class="post-metadata">

**Author:** ![Dominaezzz](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dominaezzz](https://discuss.kotlinlang.org/u/Dominaezzz)\
**Post date:** [April 29, 2020, 4:26pm UTC](https://discuss.kotlinlang.org/t/how-to-determine-linkeropts-at-build-time/17402/2 "2020-04-29T16:26:39Z")

</div>

You can either,

1. Not specify a link path and leave consumers of you library to specify it themselves, since they know their system.
2. List all paths you’re currently aware in the link line.

Relevant issue here [library search path · Issue #1534 · JetBrains/kotlin-native · GitHub](https://github.com/JetBrains/kotlin-native/issues/1534) .

---

<div class="post-metadata">

**Author:** ![Loke](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Loke](https://discuss.kotlinlang.org/u/Loke)\
**Post date:** [April 30, 2020, 2:32pm UTC](https://discuss.kotlinlang.org/t/how-to-determine-linkeropts-at-build-time/17402/3 "2020-04-30T14:32:11Z")

</div>

Thank you. I decided to add code to dynamically compute the linker options. For the regular build, this was rather easy as all I had to do was to add a `linkerOpts` parameter to the `linuxX64.binaries.executable` group.

When I do this, building the main binary works, but the tests fail because the test binary is not built with these flags. According to the documentation, I have to use `linuxX64.binaries.test` instead, like this:

```
linuxX64("linux") {
    binaries {
        executable {
            linkerOpts = ...
        }
        test {
            linkerOpts = ...
        }
    }
}

```

But when I do this, even if the `test` group is completely empty, I get the following error from Gradle:

```
Build file '/home/elias/prog/array_kotlin/array/build.gradle' line: 46

A problem occurred evaluating project ':array'.
> Cannot create binary debugTest: binary with such a name already exists

```

How can I add linker options programmatically to both the normal and test builds?

---

<div class="post-metadata">

**Author:** ![Loke](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Loke](https://discuss.kotlinlang.org/u/Loke)\
**Post date:** [April 30, 2020, 3:17pm UTC](https://discuss.kotlinlang.org/t/how-to-determine-linkeropts-at-build-time/17402/4 "2020-04-30T15:17:28Z")

</div>

I experimented a bit more, and the only way by which I seem to be able to specify linker options to the test target seems to be the following:

```
linuxX64("linux") {
    compilations.test.kotlinOptions {
        freeCompilerArgs = ["-linker-options", "-L/usr/lib64 -licui18n -licuuc -licudata"]
    }
}

```

Is this really the only way to do this?

---

<div class="post-metadata">

**Author:** ![Artyom.Degtyarev](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Artyom.Degtyarev](https://discuss.kotlinlang.org/u/Artyom.Degtyarev)\
**Post date:** [May 6, 2020, 8:27am UTC](https://discuss.kotlinlang.org/t/how-to-determine-linkeropts-at-build-time/17402/5 "2020-05-06T08:27:36Z")

</div>

Hello, @Loke! I think the first approach you tried (the one with a `linuxX64.binaries.test`) fails because of some misunderstanding. You’re specifying a new test task here, instead of changing the old one. I suppose that using `binaries.getTest(DEBUG)` would make more sense here. Or, maybe even `binaries.all`, to avoid duplicating lines. See the details [here](https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#accessing-binaries).  
The second approach seems fine, I would only recommend you to generalize it a bit - one can share this configuration between `test` and `main` compilation using `compilation.forEach {...}` method.

---

<div class="post-metadata">

**Author:** ![brunojcm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/brunojcm/32/5116_2.png) [@brunojcm](https://discuss.kotlinlang.org/u/brunojcm)\
**Post date:** [June 23, 2023, 5:55pm UTC](https://discuss.kotlinlang.org/t/how-to-determine-linkeropts-at-build-time/17402/6 "2023-06-23T17:55:24Z")

</div>

Just had a similar issue, that’s what I did:

```auto
    linuxX64 {
        compilations.all {
            compilerOptions.configure {
                freeCompilerArgs.add("-linker-options")
                freeCompilerArgs.add("-L/usr/lib")
            }
        }
    }

```
