Skip to content

Conversation

@wecing
Copy link
Contributor

@wecing wecing commented Nov 20, 2025

_NEWLIB_VERSION is only visible when any libc header is included. I ran into a weird case where during libc++ compilation, __fwd/ios.h did not see _NEWLIB_VERSION and defined off_t as long long, but in the actual user program, _NEWLIB_VERSION was visible, so the program tried to use a long int instead of long long specialization of a template function that is provided by libc++.a, and caused linking failure.

The new cmake option was also used in another PR that I created; see #167962.

_NEWLIB_VERSION is only visible when any libc header is included. I ran
into a weird case where during libc++ compilation, __fwd/ios.h did not
see _NEWLIB_VERSION and defined off_t as `long long`, but in the actual
user program, _NEWLIB_VERSION was visible, so the program tried to use a
`long int` instead of `long long` specialization of a template function
that is provided by libc++.a, and caused linking failure.

The new cmake option was also used in another PR that I created; see
llvm#167962.
@wecing wecing requested a review from jroelofs November 20, 2025 21:59
@wecing wecing requested a review from a team as a code owner November 20, 2025 21:59
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 20, 2025

@llvm/pr-subscribers-libcxx

Author: Chenguang Wang (wecing)

Changes

_NEWLIB_VERSION is only visible when any libc header is included. I ran into a weird case where during libc++ compilation, __fwd/ios.h did not see _NEWLIB_VERSION and defined off_t as long long, but in the actual user program, _NEWLIB_VERSION was visible, so the program tried to use a long int instead of long long specialization of a template function that is provided by libc++.a, and caused linking failure.

The new cmake option was also used in another PR that I created; see #167962.


Full diff: https://github.com/llvm/llvm-project/pull/168952.diff

3 Files Affected:

  • (modified) libcxx/CMakeLists.txt (+2)
  • (modified) libcxx/include/__config_site.in (+1)
  • (modified) libcxx/include/__fwd/ios.h (+1-1)
diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt
index 1423b6713fd35..fabfb126d06f3 100644
--- a/libcxx/CMakeLists.txt
+++ b/libcxx/CMakeLists.txt
@@ -298,6 +298,7 @@ endif()
 
 # Feature options -------------------------------------------------------------
 option(LIBCXX_HAS_MUSL_LIBC "Build libc++ with support for the Musl C library" OFF)
+option(LIBCXX_HAS_NEWLIB_LIBC "Build libc++ with support for the Newlib C library" OFF)
 option(LIBCXX_HAS_PTHREAD_API "Ignore auto-detection and force use of pthread API" OFF)
 option(LIBCXX_HAS_WIN32_THREAD_API "Ignore auto-detection and force use of win32 thread API" OFF)
 option(LIBCXX_HAS_EXTERNAL_THREAD_API
@@ -754,6 +755,7 @@ config_define(${LIBCXX_HAS_PTHREAD_API} _LIBCPP_HAS_THREAD_API_PTHREAD)
 config_define(${LIBCXX_HAS_EXTERNAL_THREAD_API} _LIBCPP_HAS_THREAD_API_EXTERNAL)
 config_define(${LIBCXX_HAS_WIN32_THREAD_API} _LIBCPP_HAS_THREAD_API_WIN32)
 config_define(${LIBCXX_HAS_MUSL_LIBC} _LIBCPP_HAS_MUSL_LIBC)
+config_define(${LIBCXX_HAS_NEWLIB_LIBC} _LIBCPP_HAS_NEWLIB_LIBC)
 config_define_if(LIBCXX_NO_VCRUNTIME _LIBCPP_NO_VCRUNTIME)
 config_define(${LIBCXX_ENABLE_FILESYSTEM} _LIBCPP_HAS_FILESYSTEM)
 config_define(${LIBCXX_ENABLE_RANDOM_DEVICE} _LIBCPP_HAS_RANDOM_DEVICE)
diff --git a/libcxx/include/__config_site.in b/libcxx/include/__config_site.in
index 6dcca1849a96c..6521cf7c51c96 100644
--- a/libcxx/include/__config_site.in
+++ b/libcxx/include/__config_site.in
@@ -17,6 +17,7 @@
 #cmakedefine01 _LIBCPP_HAS_MONOTONIC_CLOCK
 #cmakedefine01 _LIBCPP_HAS_TERMINAL
 #cmakedefine01 _LIBCPP_HAS_MUSL_LIBC
+#cmakedefine01 _LIBCPP_HAS_NEWLIB_LIBC
 #cmakedefine01 _LIBCPP_HAS_THREAD_API_PTHREAD
 #cmakedefine01 _LIBCPP_HAS_THREAD_API_EXTERNAL
 #cmakedefine01 _LIBCPP_HAS_THREAD_API_WIN32
diff --git a/libcxx/include/__fwd/ios.h b/libcxx/include/__fwd/ios.h
index 831624f4b1c57..96505e615693c 100644
--- a/libcxx/include/__fwd/ios.h
+++ b/libcxx/include/__fwd/ios.h
@@ -31,7 +31,7 @@ using wios = basic_ios<wchar_t>;
 template <class _CharT, class _Traits>
 class _LIBCPP_PREFERRED_NAME(ios) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wios)) basic_ios;
 
-#if defined(_NEWLIB_VERSION)
+#if _LIBCPP_HAS_NEWLIB_LIBC
 // On newlib, off_t is 'long int'
 using streamoff = long int; // for char_traits in <string>
 #else

@wecing wecing requested a review from mplatings November 21, 2025 22:37
@wecing
Copy link
Contributor Author

wecing commented Nov 21, 2025

Added @mplatings for the CI changes

Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure #147956 fixes your problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants