CMake: better LINKER_LANGUAGE detect for C++ library with Fortran main

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

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.

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 is set only by internal CMake script with some arbitrary values:

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.

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()

Please file a feature request issue.

https://gitlab.kitware.com/cmake/cmake/-/merge_requests/8871

1 Like