[libcxx-commits] [libcxx] [libcxx] Omit default library directives when building static libc++ on Windows (PR #238238)

Petr Hosek via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 2 23:17:02 PST 2026


https://github.com/petrhosek created https://github.com/llvm/llvm-project/pull/238238

Pass -fms-omit-default-lib when compiling static libc++ targets on
Windows MSVC so object files in libc++.lib do not embed hardcoded CRT
--dependent-lib directives, and suppress libc++ pragma comment(lib)
auto-linking when _VC_NODEFAULTLIB is defined.


>From ac688628b423ecda7293079ea93593719b25b5b4 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Sat, 31 Oct 2026 17:48:56 -0700
Subject: [PATCH] [libcxx] Omit default library directives when building static
 libc++ on Windows

Pass -fms-omit-default-lib when compiling static libc++ targets on
Windows MSVC so object files in libc++.lib do not embed hardcoded CRT
--dependent-lib directives, and suppress libc++ pragma comment(lib)
auto-linking when _VC_NODEFAULTLIB is defined.
---
 libcxx/CMakeLists.txt           | 4 ++++
 libcxx/include/__config         | 4 ++--
 libcxx/include/__cxx03/__config | 4 ++--
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt
index 8841f5d07aaf6c..eb4ebb2a04a143 100644
--- a/libcxx/CMakeLists.txt
+++ b/libcxx/CMakeLists.txt
@@ -731,6 +731,10 @@ function(cxx_add_windows_flags target)
                                  # Use the ISO conforming behaviour for conversion
                                  # in printf, scanf.
                                  _CRT_STDIO_ISO_WIDE_SPECIFIERS)
+    get_target_property(target_type ${target} TYPE)
+    if("${target_type}" STREQUAL "STATIC_LIBRARY")
+      target_add_compile_flags_if_supported(${target} PRIVATE -fms-omit-default-lib)
+    endif()
   endif()
 endfunction()
 
diff --git a/libcxx/include/__config b/libcxx/include/__config
index ade43d42dc283d..20006e6c091f98 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -277,7 +277,7 @@ typedef __char32_t char32_t;
 #  define _LIBCPP_POP_MACROS _Pragma("pop_macro(\"min\")") _Pragma("pop_macro(\"max\")") _Pragma("pop_macro(\"refresh\")") _Pragma("pop_macro(\"move\")") _Pragma("pop_macro(\"erase\")")
 // clang-format on
 
-#  ifndef _LIBCPP_NO_AUTO_LINK
+#  if !defined(_LIBCPP_NO_AUTO_LINK) && !defined(_VC_NODEFAULTLIB)
 #    if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
 #      if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
 #        pragma comment(lib, "c++.lib")
@@ -285,7 +285,7 @@ typedef __char32_t char32_t;
 #        pragma comment(lib, "libc++.lib")
 #      endif
 #    endif // defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
-#  endif   // _LIBCPP_NO_AUTO_LINK
+#  endif   // !defined(_LIBCPP_NO_AUTO_LINK) && !defined(_VC_NODEFAULTLIB)
 
 // c8rtomb() and mbrtoc8() were added in C++20 and C23. Support for these
 // functions is gradually being added to existing C libraries. The conditions
diff --git a/libcxx/include/__cxx03/__config b/libcxx/include/__cxx03/__config
index 46c7c4440e7b93..c8930e6edf5f0f 100644
--- a/libcxx/include/__cxx03/__config
+++ b/libcxx/include/__cxx03/__config
@@ -798,7 +798,7 @@ typedef __char32_t char32_t;
 #  define _LIBCPP_POP_MACROS _Pragma("pop_macro(\"min\")") _Pragma("pop_macro(\"max\")") _Pragma("pop_macro(\"refresh\")") _Pragma("pop_macro(\"move\")") _Pragma("pop_macro(\"erase\")")
 // clang-format on
 
-#  ifndef _LIBCPP_NO_AUTO_LINK
+#  if !defined(_LIBCPP_NO_AUTO_LINK) && !defined(_VC_NODEFAULTLIB)
 #    if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
 #      if !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
 #        pragma comment(lib, "c++.lib")
@@ -806,7 +806,7 @@ typedef __char32_t char32_t;
 #        pragma comment(lib, "libc++.lib")
 #      endif
 #    endif // defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_BUILDING_LIBRARY)
-#  endif   // _LIBCPP_NO_AUTO_LINK
+#  endif   // !defined(_LIBCPP_NO_AUTO_LINK) && !defined(_VC_NODEFAULTLIB)
 
 // Configures the fopen close-on-exec mode character, if any. This string will
 // be appended to any mode string used by fstream for fopen/fdopen.



More information about the libcxx-commits mailing list