Skip to content

[flang] MSVC runtime library selection #68017

Closed
@bradking

Description

@bradking

For targets such as x86_64-pc-windows-msvc, LLVM/Flang generates
object files for the MSVC ABI and uses MSVC-compatible tooling.

Compilers that target the MSVC ABI need to select one of the MSVC
runtime libraries. They should define preprocessor macros identifying
the selected runtime library, and add #pragma comment-style annotations
to object files to pass /defaultlib:... flags to the linker without
build system intervention. With LLVM/Flang the latter is needed even
for a trivial empty Fortran program:

>type foo.f90
program foo
end program
>flang-new foo.f90
LINK : error LNK2001: unresolved external symbol mainCRTStartup
>flang-new foo.f90 -Xlinker -defaultlib:msvcrt
(works)

MSVC toolsets provide the following runtime library variants:

  • libcmt: Multi-threaded, optimized, linked statically.
    Defines -D_MT, links /defaultlib:libcmt.
    MSVC flag: cl -MT.

  • msvcrt: Multi-threaded, optimized, linked shared.
    Defines -D_MT -D_DLL, links /defaultlib:msvcrt.
    MSVC flag: cl -MD.

  • libcmtd: Multi-threaded, debug, linked statically.
    Defines -D_MT -D_DEBUG, links /defaultlib:libcmtd.
    MSVC flag: cl -MTd.

  • msvcrtd: Multi-threaded, debug, linked shared.
    Defines -D_MT -D_DEBUG -D_DLL, links /defaultlib:msvcrtd.
    MSVC flag: cl -MDd.

LLVM/Flang should be updated as follows:

  • Add command-line flags to select a MSVC runtime library variant.
  • Provide predefined macros indicating the selected MSVC runtime library
    for the preprocessor.
  • Generate /defaultlib:... flags in object files referencing the
    selected MSVC runtime library for the linker.

For reference, LLVM/Clang does all of these when targeting the MSVC ABI.

Additionally, LLVM/Flang's own runtime libraries (Fortran*.lib)
could be provided as variants corresponding to each MSVC runtime
library variant. Currently the Fortran*.lib libraries included in
the LLVM/Flang distribution only use the msvcrt runtime library.
LLVM/Flang should then write /defaultlib:... references in object
files, to link its own Fortran*.lib runtime libraries, matching the
selected MSVC runtime library variant.

Cc: @DavidTruby

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions