Reputation: 1356
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?
Upvotes: 0
Views: 806
Reputation: 52081
-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
).
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.
Upvotes: 0