0

I have a C++ project that uses Fortran.

When compiling under GCC, I can link in Fortran statically using target_link_libraries(Project -static-libgfortran gfortran).

However, I am now compiling the same project using Clang, which means instead of gfortran I am using Flang. (flang-new version 15.0.7)

If I don't include any replacement, I get a lot of undefined symbols, like _FortranAioBeginExternalFormattedOutput.

If I try to swap out those two with just "flang", it fails, saying:

lld: error: unable to find library -lflang

LLVM has a single sentence on linking flang, which I don't follow:

"After the LLVM IR is created, the flang driver invokes LLVM’s existing infrastructure to generate object code and invoke a linker to create the executable file."

How do I link in Flang the same way I did GFortran?

Tyler Shellberg
  • 1,086
  • 11
  • 28
  • any difference if you use [`target_link_options`](https://cmake.org/cmake/help/latest/command/target_link_options.html) instead for that linker flag? It's not really a library (linker flags in `target_link_libraries` should be ok [according to the docs](https://cmake.org/cmake/help/latest/command/target_link_libraries.html), but I'm just curious to see if anything changes). – starball Mar 28 '23 at 19:34
  • `-static-libgfortran` is listed in https://clang.llvm.org/docs/ClangCommandLineReference.html#fortran-compilation-flags. What if you instead use `target_compile_options(Project PUBLIC -static-libgfortran)` (or `PRIVATE`)? – starball Mar 28 '23 at 21:13

1 Answers1

0

-static-libgfortran is listed in https://clang.llvm.org/docs/ClangCommandLineReference.html#fortran-compilation-flags. Try using target_compile_options(Project PUBLIC -static-libgfortran) (or PRIVATE).

For Classic Flang

The compiler option docs don't list a -static-libgfortran flag, but they do list a -static-flang-libs. Try using that instead. But since it's listed as a compiler option, I'd try using target_compile_options.

If you want to support both GFortran and Flang, then wrap the target_link_options / target_link_libraries calls in if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") or if(CMAKE_CXX_COMPILER_ID STREQUAL "Flang") (see docs for CMAKE_LANG_COMPILER_ID), or use the $<Fortran_COMPILER_ID:...> generator expression.

If this is a linker flag, I'd recommend target_link_options for linker options instead of target_link_libraries just for readability's sake (using target_link_libraries should work too though according to the docs).

Note: I found the info about the -static-flag-libs flag by googling ""flang" "-static-libgfortran"", where my top search result was this SciVision post, which showed the -static-flang-libs flag. Then a google of ""flang" "-static-flang-libs"" led me to the Flang GitHub. Tip for googling flags that start with a dash: enclose in double-quotes to avoid it being used as a exclusion operator.

starball
  • 20,030
  • 7
  • 43
  • 238
  • 1
    The link you mention is for Classic Flang. There was a schism and we have two Flangs. The "classic flang" that requires a special fork of LLVM, and there is "new flang" which is part of main LLVM. Getting classic flang would involve recompiling all of LLVM, which I am not keen to do for many reasons. For thoroughness, neither -static-flang-libs or flang are recognized by the linker. Those exist for classic flang, not "new flang". – Tyler Shellberg Mar 28 '23 at 20:07