# Custom Clang version with Kotlin/Native

**URL:** <https://discuss.kotlinlang.org/t/custom-clang-version-with-kotlin-native/28150>\
**Category:** Native\
**Created:** [February 15, 2024, 1:57pm UTC](https://discuss.kotlinlang.org/t/custom-clang-version-with-kotlin-native/28150 "2024-02-15T13:57:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paxbun](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@paxbun](https://discuss.kotlinlang.org/u/paxbun)\
**Post date:** [February 15, 2024, 1:57pm UTC](https://discuss.kotlinlang.org/t/custom-clang-version-with-kotlin-native/28150/1 "2024-02-15T13:57:29Z")

</div>

Hi, I’m trying to link a Rust MinGW static library to a Kotlin/Native MinGW library on macOS. I’m using Rust 1.75 with Kotlin 1.9.21.  
I encounter a linker error like the following:

```auto
e: /Users/paxbun/.konan/dependencies/apple-llvm-20200714-macos-aarch64-essentials/bin/clang++ invocation reported errors

The /Users/paxbun/.konan/dependencies/apple-llvm-20200714-macos-aarch64-essentials/bin/clang++ command returned non-zero exit code: 1.
output:
lld-link: warning: ignoring unknown argument: -exclude-symbols:___chkstk_ms
lld-link: error: -exclude-symbols:___chkstk_ms is not allowed in .drectve
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

FAILURE: Build failed with an exception.

```

and it seems that the flag was added in Clang 16 and then ported to Clang 15.

> <https://github.com/llvm/llvm-project/issues/57076>
>
> I'd suggest backporting d1da6469f9ea9b078276ee2e098241f0440468be ("\[LLD\] \[MinGW\]… Implement the --exclude-symbols option") and 5d513ef6cf4646e64bbb1d5f8610afd530964588 ("\[LLD\] \[COFF\] Add support for a new, mingw specific embedded directive -exclude-symbols:") to the 15.x release.
> 
> As they touch upon release notes, there will be conflicts in backporting, so I'll manually make a backports branch.

I want to use K/N with Clang 16, which is installed on my machine. Is there any way to use a user-specified version of Clang when using K/N?

---

<div class="post-metadata">

**Author:** ![paxbun](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@paxbun](https://discuss.kotlinlang.org/u/paxbun)\
**Post date:** [February 16, 2024, 1:38am UTC](https://discuss.kotlinlang.org/t/custom-clang-version-with-kotlin-native/28150/2 "2024-02-16T01:38:58Z")

</div>

I found this docs in GH: [kotlin/kotlin-native/HACKING.md at 7a13023b1463b651904440e3ffbedf1bfa379c9b · JetBrains/kotlin · GitHub](https://github.com/JetBrains/kotlin/blob/7a13023b1463b651904440e3ffbedf1bfa379c9b/kotlin-native/HACKING.md#compiler-environment-variables)

I’ll leave the results here after some experiments.

---

<div class="post-metadata">

**Author:** ![paxbun](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@paxbun](https://discuss.kotlinlang.org/u/paxbun)\
**Post date:** [February 18, 2024, 4:53am UTC](https://discuss.kotlinlang.org/t/custom-clang-version-with-kotlin-native/28150/3 "2024-02-18T04:53:06Z")

</div>

```kotlin
mingwX64().binaries.executable {
    compilerOptions {
        freeCompilerArgs.add("-Xoverride-konan-properties=linker.macos_arm64-mingw_x64=/opt/homebrew/opt/llvm/bin/ld.lld")
    }
}

```

Overriding the Konan `linker.<host>-<target>` option did work. Issue resolved! 😄

BTW, building my own version of LLVM did not work, as Gradle crashes while running linkDebugExecutableMingwX64. It seems linkDebugExecutableMingwX64 runs the compiler within the Gradle process (so it calls a shared library, I assume), and the crash occurs inside the shared library, which is why Gradle just terminates without any Java exception. Maybe using a different LLVM version entirely is incompatible with the Kotlin Gradle plugin.
