[llvm] [flang-rt] Avoid duplicate definition of `std::__libcpp_verbose_abort` (PR #175551)

Markus Mützel via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 20 09:19:40 PDT 2026


https://github.com/mmuetzel updated https://github.com/llvm/llvm-project/pull/175551

>From 2bc832c052051a270931ffc9fa0e0abb108a712b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Markus=20M=C3=BCtzel?= <markus.muetzel at gmx.de>
Date: Sun, 11 Jan 2026 19:57:00 +0100
Subject: [PATCH 1/3] flang-rt: Avoid duplicate definition of
 `std::__libcpp_verbose_abort`

If a project depends on the Fortran runtime and on libc++, linking fails
because `std::__libcpp_verbose_abort` is defined in both libraries.

Avoid that duplicate definition by defining `_LIBCPP_VERBOSE_ABORT` before
including any C++ headers and by renaming that symbol in the Fortran runtime
to `flang_rt_verbose_abort`.

The function that is modified was originally introduced in D158957 to solve an
undefined symbol error when linking pure-Fortran projects with the Flang
runtime.
Providing a definition for that symbol in the Flang runtime might work
correctly for ELF or Mach-O if that symbol has weak linkage in libc++. But at
least for COFF, this now causes multiple-definition errors for projects that
are linking to the Flang runtime and to libc++.

The linker errors before this change for Windows/MinGW using Clang+Flang+lld
look like this:
```
ld.lld: error: duplicate symbol: std::__1::__libcpp_verbose_abort(char const*, ...)
>>> defined at libflang_rt.runtime.a(io-api-minimal.cpp.obj)
>>> defined at libc++.dll.a(libc++.dll)
```
---
 flang-rt/cmake/modules/AddFlangRT.cmake |  6 ++++++
 flang-rt/lib/runtime/io-api-minimal.cpp |  7 +++----
 flang-rt/lib/runtime/io-api-minimal.h   | 12 ++++++++++++
 3 files changed, 21 insertions(+), 4 deletions(-)
 create mode 100644 flang-rt/lib/runtime/io-api-minimal.h

diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index 7c416ea77b88c..188371512fef6 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -323,6 +323,12 @@ function (add_flangrt_library name)
           "$<$<COMPILE_LANGUAGE:CXX>:-Wp,-U_LIBCPP_ENABLE_ASSERTIONS>")
     endif ()
 
+    # Use own function in place of `std::__libcpp_verbose_abort` to avoid an
+    # unwanted dependency on the symbol provided by libc++.
+    target_compile_options(${tgtname} PRIVATE
+      $<$<COMPILE_LANGUAGE:CXX>:"-D_LIBCPP_VERBOSE_ABORT(...)=flang_rt_verbose_abort(__VA_ARGS__)" -include "${FLANG_RT_SOURCE_DIR}/lib/runtime/io-api-minimal.h">
+      )
+
     # Non-GTest unittests depend on LLVMSupport
     if (ARG_LINK_TO_LLVM)
       if (LLVM_LINK_LLVM_DYLIB)
diff --git a/flang-rt/lib/runtime/io-api-minimal.cpp b/flang-rt/lib/runtime/io-api-minimal.cpp
index fdf7183ed5176..491bf4dc31e60 100644
--- a/flang-rt/lib/runtime/io-api-minimal.cpp
+++ b/flang-rt/lib/runtime/io-api-minimal.cpp
@@ -147,11 +147,10 @@ bool IODEF(OutputLogical)(Cookie cookie, bool truth) {
 } // namespace Fortran::runtime::io
 
 #if defined(_LIBCPP_VERBOSE_ABORT)
-// Provide own definition for `std::__libcpp_verbose_abort` to avoid dependency
-// on the version provided by libc++.
+// Provide function that is used in place of `std::__libcpp_verbose_abort` to
+// avoid dependency on the symbol provided by libc++.
 
-void std::__libcpp_verbose_abort(char const *format, ...) noexcept(
-    noexcept(std::__libcpp_verbose_abort(""))) {
+void flang_rt_verbose_abort(char const *format, ...) {
   va_list list;
   va_start(list, format);
   std::vfprintf(stderr, format, list);
diff --git a/flang-rt/lib/runtime/io-api-minimal.h b/flang-rt/lib/runtime/io-api-minimal.h
new file mode 100644
index 0000000000000..aa31614f2302a
--- /dev/null
+++ b/flang-rt/lib/runtime/io-api-minimal.h
@@ -0,0 +1,12 @@
+//===-- lib/runtime/io-api-minimal.h --------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Declare function that is used in place of `std::__libcpp_verbose_abort` to
+// avoid dependency on the symbol provided by libc++.
+
+void flang_rt_verbose_abort(char const *format, ...);

>From 36de7b4d808938d668699ac22179c03628f7715c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Markus=20M=C3=BCtzel?= <markus.muetzel at gmx.de>
Date: Thu, 19 Mar 2026 19:26:03 +0100
Subject: [PATCH 2/3] Apply changes from review feedback.

---
 flang-rt/cmake/modules/AddFlangRT.cmake | 40 +++++++++----------------
 flang-rt/lib/runtime/io-api-minimal.h   | 12 --------
 flang-rt/lib/runtime/stl-overrides.h    | 37 +++++++++++++++++++++++
 3 files changed, 51 insertions(+), 38 deletions(-)
 delete mode 100644 flang-rt/lib/runtime/io-api-minimal.h
 create mode 100644 flang-rt/lib/runtime/stl-overrides.h

diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index 188371512fef6..6d8d38f5569d9 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -238,34 +238,28 @@ function (add_flangrt_library name)
           $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti -funwind-tables -fno-asynchronous-unwind-tables>
         )
 
-      # We define our own _GLIBCXX_THROW_OR_ABORT here because, as of
-      # GCC 15.1, the libstdc++ header file <bits/c++config> uses
-      # (void)_EXC in its definition of _GLIBCXX_THROW_OR_ABORT to
-      # silence a warning.
-      #
-      # This is a problem for us because some compilers, specifically
-      # clang, do not always optimize away that (void)_EXC even though
-      # it is unreachable since it occurs after a call to
-      # _builtin_abort().  Because _EXC is typically an object derived
-      # from std::exception, (void)_EXC, when not optimized away,
-      # calls std::exception methods defined in the libstdc++ shared
-      # library.  We shouldn't link against that library since our
-      # build version may conflict with the version used by a hybrid
-      # Fortran/C++ application.
-      #
-      # Redefining _GLIBCXX_THROW_OR_ABORT in this manner is not
-      # supported by the maintainers of libstdc++, so future changes
-      # to libstdc++ may require future changes to this build script
-      # and/or future changes to the Fortran runtime source code.
-      target_compile_options(${tgtname} PUBLIC "-D_GLIBCXX_THROW_OR_ABORT(_EXC)=(__builtin_abort())")
+      # Include a `config.h`-style header at the top of all compilation units.
+      target_compile_options(${tgtname} PRIVATE
+          $<$<COMPILE_LANGUAGE:CXX>:-include "${FLANG_RT_SOURCE_DIR}/lib/runtime/stl-overrides.h">
+        )
     elseif (MSVC)
       target_compile_options(${tgtname} PRIVATE
           $<$<COMPILE_LANGUAGE:CXX>:/EHs-c- /GR->
         )
+
+      # Include a `config.h`-style header at the top of all compilation units.
+      target_compile_options(${tgtname} PRIVATE
+          $<$<COMPILE_LANGUAGE:CXX>:/FI "${FLANG_RT_SOURCE_DIR}/lib/runtime/stl-overrides.h">
+        )
     elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
       target_compile_options(${tgtname} PRIVATE
           $<$<COMPILE_LANGUAGE:CXX>:-qnoeh -qnortti>
         )
+
+      # Include a `config.h`-style header at the top of all compilation units.
+      target_compile_options(${tgtname} PRIVATE
+          $<$<COMPILE_LANGUAGE:CXX>:-qinclude="${FLANG_RT_SOURCE_DIR}/lib/runtime/stl-overrides.h">
+        )
     endif ()
 
     # Add target specific options if necessary.
@@ -323,12 +317,6 @@ function (add_flangrt_library name)
           "$<$<COMPILE_LANGUAGE:CXX>:-Wp,-U_LIBCPP_ENABLE_ASSERTIONS>")
     endif ()
 
-    # Use own function in place of `std::__libcpp_verbose_abort` to avoid an
-    # unwanted dependency on the symbol provided by libc++.
-    target_compile_options(${tgtname} PRIVATE
-      $<$<COMPILE_LANGUAGE:CXX>:"-D_LIBCPP_VERBOSE_ABORT(...)=flang_rt_verbose_abort(__VA_ARGS__)" -include "${FLANG_RT_SOURCE_DIR}/lib/runtime/io-api-minimal.h">
-      )
-
     # Non-GTest unittests depend on LLVMSupport
     if (ARG_LINK_TO_LLVM)
       if (LLVM_LINK_LLVM_DYLIB)
diff --git a/flang-rt/lib/runtime/io-api-minimal.h b/flang-rt/lib/runtime/io-api-minimal.h
deleted file mode 100644
index aa31614f2302a..0000000000000
--- a/flang-rt/lib/runtime/io-api-minimal.h
+++ /dev/null
@@ -1,12 +0,0 @@
-//===-- lib/runtime/io-api-minimal.h --------------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// Declare function that is used in place of `std::__libcpp_verbose_abort` to
-// avoid dependency on the symbol provided by libc++.
-
-void flang_rt_verbose_abort(char const *format, ...);
diff --git a/flang-rt/lib/runtime/stl-overrides.h b/flang-rt/lib/runtime/stl-overrides.h
new file mode 100644
index 0000000000000..9b8caa9b6d6ac
--- /dev/null
+++ b/flang-rt/lib/runtime/stl-overrides.h
@@ -0,0 +1,37 @@
+//===-- lib/runtime/stl-overrides.h -----------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// This is a `config-h`-style header that is meant to be included as the first
+// header in all compilation units of the Flang runtime library.  This is
+// currently done using command line flags in `AddFlangRT.cmake`.
+
+// We define our own _GLIBCXX_THROW_OR_ABORT here because, as of GCC 15.1, the
+// libstdc++ header file <bits/c++config> uses (void)_EXC in its definition of
+// _GLIBCXX_THROW_OR_ABORT to silence a warning.
+//
+// This is a problem for us because some compilers, specifically clang, do not
+// always optimize away that (void)_EXC even though it is unreachable since it
+// occurs after a call to _builtin_abort().  Because _EXC is typically an
+// object derived from std::exception, (void)_EXC, when not optimized away,
+// calls std::exception methods defined in the libstdc++ shared library.  We
+// shouldn't link against that library since our build version may conflict
+// with the version used by a hybrid Fortran/C++ application.
+//
+// Redefining _GLIBCXX_THROW_OR_ABORT in this manner is not supported by the
+// maintainers of libstdc++, so future changes to libstdc++ may require future
+// changes to this build script and/or future changes to the Fortran runtime
+// source code.
+#define _GLIBCXX_THROW_OR_ABORT(_EXC) (__builtin_abort())
+
+// Declare function that is used in place of `std::__libcpp_verbose_abort` to
+// avoid dependency on the symbol provided by libc++.
+#ifndef _LIBCPP_VERBOSE_ABORT
+#define _LIBCPP_VERBOSE_ABORT(...) flang_rt_verbose_abort(__VA_ARGS__)
+void flang_rt_verbose_abort(char const *format, ...)
+    __attribute__((format(printf, 1, 2)));
+#endif

>From f5dfa3dc508fe771004ffe3b3be0d338ea44a1fd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Markus=20M=C3=BCtzel?= <markus.muetzel at gmx.de>
Date: Fri, 20 Mar 2026 17:02:15 +0100
Subject: [PATCH 3/3] Remove spaces in expression to avoid CMake warnings.

---
 flang-rt/cmake/modules/AddFlangRT.cmake | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake
index 6d8d38f5569d9..988f6e3933f72 100644
--- a/flang-rt/cmake/modules/AddFlangRT.cmake
+++ b/flang-rt/cmake/modules/AddFlangRT.cmake
@@ -240,7 +240,7 @@ function (add_flangrt_library name)
 
       # Include a `config.h`-style header at the top of all compilation units.
       target_compile_options(${tgtname} PRIVATE
-          $<$<COMPILE_LANGUAGE:CXX>:-include "${FLANG_RT_SOURCE_DIR}/lib/runtime/stl-overrides.h">
+          "$<$<COMPILE_LANGUAGE:CXX>:-include${FLANG_RT_SOURCE_DIR}/lib/runtime/stl-overrides.h>"
         )
     elseif (MSVC)
       target_compile_options(${tgtname} PRIVATE
@@ -249,7 +249,7 @@ function (add_flangrt_library name)
 
       # Include a `config.h`-style header at the top of all compilation units.
       target_compile_options(${tgtname} PRIVATE
-          $<$<COMPILE_LANGUAGE:CXX>:/FI "${FLANG_RT_SOURCE_DIR}/lib/runtime/stl-overrides.h">
+          "$<$<COMPILE_LANGUAGE:CXX>:/FI${FLANG_RT_SOURCE_DIR}/lib/runtime/stl-overrides.h>"
         )
     elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
       target_compile_options(${tgtname} PRIVATE
@@ -258,7 +258,7 @@ function (add_flangrt_library name)
 
       # Include a `config.h`-style header at the top of all compilation units.
       target_compile_options(${tgtname} PRIVATE
-          $<$<COMPILE_LANGUAGE:CXX>:-qinclude="${FLANG_RT_SOURCE_DIR}/lib/runtime/stl-overrides.h">
+          "$<$<COMPILE_LANGUAGE:CXX>:-qinclude=${FLANG_RT_SOURCE_DIR}/lib/runtime/stl-overrides.h>"
         )
     endif ()
 



More information about the llvm-commits mailing list