<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/92496>92496</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang][driver] impossible to combine `-shared` with linking to `Fortran_main` (flang 18.1)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          h-vetinari
      </td>
    </tr>
</table>

<pre>
    https://github.com/llvm/llvm-project/commit/9d6837d595719904720e5ff68ec1f1a2665bdc2f removed the ability to link to `Fortran_main` by default when building `-shared` libraries. However, building such libraries is how SciPy works under the hood. Shared libraries coming from C/C++/Fortran, which are then accessed from Python.

I managed to build SciPy with flang 17 (bit of a [long story](https://labs.quansight.org/blog/building-scipy-with-flang)), and was now trying to update to flang 18. Aside from issues like #92459, I cannot figure out how to combine `-shared` with actually linking to `Fortran_main`. Not that I care particularly about the philosophical aspects of that, but very practically, I get missing symbols without it
```
[1376/1475] Linking target scipy/linalg/_interpolative.cp310-win_amd64.pyd
FAILED: scipy/linalg/_interpolative.cp310-win_amd64.pyd 
"flang-new" -Wl,/OUT:[...objects...] "-Wl,/nologo" "-Wl,/OPT:REF" "-Wl,/DLL" "-Wl,/IMPLIB:scipy\linalg\_interpolative.cp310-win_amd64.lib" "--target=x86_64-pc-windows-msvc" "-fms-runtime-lib=dll" "-fuse-ld=lld" "-Wl,-defaultlib:%PREFIX%/lib/clang/18/lib/windows/clang_rt.builtins-x86_64.lib" "-D_CRT_SECURE_NO_WARNINGS" "-D_MT" "-D_DLL" "--target=x86_64-pc-windows-msvc" "scipy/lib_fortranobject.a" "-Wl,--version-script=C:/bld/scipy-split_1715896809937/work/scipy/_build_utils/link-version-pyinit.map" "%PREFIX%/lib/lapack.lib" "%PREFIX%/lib/blas.lib" "%PREFIX%\libs\python310.lib" "-lFortranRuntime" "-lFortranDecimal"
lld-link: warning: ignoring unknown argument '--version-script=C:/bld/scipy-split_1715896809937/work/scipy/_build_utils/link-version-pyinit.map'
lld-link: error: undefined symbol: _QQmain
```

Surprisingly, even _with_ a `"-lFortran_main"` at the end of that invocation, I get the same missing symbols. I've tried a couple of variations but could get none of them to work. The `-fno-fortran-main` flag mentioned in that PR doesn't [seem](https://releases.llvm.org/18.1.0/tools/flang/docs/FlangCommandLineReference.html#cmdoption-flang-fno-fortran-main) to have a positive formulation that _does_ enforce linkage to `Fortran_main`.

As an aside, this is not just for SciPy, relatedly I'm also trying to teach meson how to handle llvm-flang, and this is one of the things I need to figure out how to do. Finally, a lot of the changes from the commit mentioned at the top have been undone in https://github.com/llvm/llvm-project/commit/8d5386669ed63548daf1bee415596582d6d78d7d, but that came after flang 18. It needs to be possible to do this with released versions (or in the extreme case that it isn't, I need to error out in meson and tell people to upgrade their compiler).

CC @mjklemm @DavidTruby 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV01v4zjS_jX0hZAgUZZkHXJI7Pb7Bsh0Z9I9mL0ZlFiy2KFILUnZ43-_KFJO3B8zuzuXBQybporF-nzqEXdOHjXAHSkfSLlb8dkPxt4NyQm81NzKVWvE5W7wfnKkuCdsT9j-KP0wt2lnRsL2Sp2uP8lkzVfoPGH7zoyjxEUjqk1Ri7Ip67xpsnXNMij7vtpAl_c5Z1VVtqJjPbUwmhMI6gegvJVK-gv1hiqpX_GXVNneWG-5PoxcalJltL1QAT2flafnATRtZ6mE1EeUTdzALQgUU7K13EpwKf1_c4YTWMK278Ju7oZ3GSodHcyZfu7k84WejX11dNYCbLBrMEak9HNQfXOmMyNq6q0Z6Zaw_Zawh_DZLybjhedBdgPlFlCTprzrwDkQ8dTzxQ9GpyTbkew-fj_SkWt-xIiYaO3VKOkH2iuujzSvKWGbVnpqesopKR-UQZe8sRdS7gjbfJs4xVuX_nPm2snj4FNjj4TtW2XCzxKQxHVyuiR4SxJuIawJny3lWtAzd1SbM_X2g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