# CMake: better LINKER\_LANGUAGE detect for C++ library with Fortran main

**URL:** https://discourse.cmake.org/t/cmake-better-linker-language-detect-for-c-library-with-fortran-main/9179
**Category:** Development
**Created:** [October 11, 2023, 4:08pm UTC](https://discourse.cmake.org/t/cmake-better-linker-language-detect-for-c-library-with-fortran-main/9179 "2023-10-11T16:08:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [October 11, 2023, 4:08pm UTC](https://discourse.cmake.org/t/cmake-better-linker-language-detect-for-c-library-with-fortran-main/9179/1 "2023-10-11T16:08:39Z")

</div>

tl;dr: I’d like to improve LINKER\_LANGUAGE auto-detect for C++ library with Fortran main program.

Across compilers and OS, CMake is good at detecting the appropriate linker\_language EXCEPT for C++ library with Fortran main program.  
For those targets, I always have to set target property

```cmake
set_property(TARGET fortran_main PROPERTY LINKER_LANGUAGE Fortran)

```

I assume that until recent years, using C++ libraries with Fortran main wasn’t so common. However a key use for my work is [C++17 filesystem library in Fortran programs](https://github.com/scivision/fortran-filesystem).

For `try_run()` I can’t set the linker\_language without CMake script access since only the source files are specified. Rather than mess with `try_run()` internals, maybe it’s better to **improve CMake’s linker language detection**.

The reason I’d like to use `try_run()` is to verify C++ exception handling is working at CMake configure time, which I’ve found needs to be done with a test program.

## Details

It looks like [`CMAKE_<LANG>_LINKER_PREFERENCE`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_LINKER_PREFERENCE.html) is set only by internal CMake script with some arbitrary values:

- [https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/CMakeCCompiler.cmake.in](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/CMakeCCompiler.cmake.in)
- [https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/CMakeCXXCompiler.cmake.in](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/CMakeCXXCompiler.cmake.in)
- [https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/CMakeFortranCompiler.cmake.in](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/CMakeFortranCompiler.cmake.in)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 11, 2023, 4:13pm UTC](https://discourse.cmake.org/t/cmake-better-linker-language-detect-for-c-library-with-fortran-main/9179/2 "2023-10-11T16:13:35Z")

</div>

CMake has no idea which file contains `main`, so auto-detection will always be heuristics. We can’t defer until build time with a scanner because the command like may be completely different. Exposing a way to set the linker language in `try_*` sounds best to me.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [October 11, 2023, 4:15pm UTC](https://discourse.cmake.org/t/cmake-better-linker-language-detect-for-c-library-with-fortran-main/9179/3 "2023-10-11T16:15:02Z")

</div>

OK as I updated my post I see the same thing. Yes I don’t mind that only new CMake would have this try\_run() linker\_language option as it would be for advanced debugging on new systems (e.g. specific HPC clusters).

Since try\_run() is specifically for single test target, such an option is very natural for try\_run()

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 11, 2023, 4:15pm UTC](https://discourse.cmake.org/t/cmake-better-linker-language-detect-for-c-library-with-fortran-main/9179/4 "2023-10-11T16:15:47Z")

</div>

Please file a feature request issue.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [October 12, 2023, 5:59pm UTC](https://discourse.cmake.org/t/cmake-better-linker-language-detect-for-c-library-with-fortran-main/9179/5 "2023-10-12T17:59:15Z")

</div>

[https://gitlab.kitware.com/cmake/cmake/-/merge\_requests/8871](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/8871)
