Libcxx and locale (original) (raw)

We are porting libcxx to an embedded space. There’s no/limited need for locales here.
Disabling locales, LIBCXX_ENABLE_LOCALIZATION=OFF, is causing some limitations in the use of libcxx, for example iostream doesn’t work.
I am wondering if there’s a plan to pull out the localization dependency from libcxx?

I can’t speak for the libcxx maintainers, but to clarify your request, I presume the intent would be that iostreams (and anything else locale dependent) would behave as-if the “C” locale was always in effect?

There is interaction between C++ locales and C locales. How does the C library work in your environment with respect to locales? Does it always behave as if the “C” locale is in effect?

We have locale support in our C library which is C99 compliant. But it looks like libcxx requires POSIX locale.

I don’t know what a current LIBCXX requires, but we ported a LLVM 10 vintage LIBCXX to OpenVMS. We also have just C99 locale and not POSIX locale (what’s the difference?) Works fine for us.

That’s good to know.
When we ported LIBCXX 16.0.3 we got an error that locale_t type not defined.
And in our C library locale_t is defined under any one of the following macros:
_POSIX_SOURCE _POSIX_C_SOURCE _XOPEN_SOURCE _GNU_SOURCE
_BSD_SOURCE.
I think POSIX standard defines more locale types than C99.
May I ask what C library you are using?
We use musl C.

Take a look at include/__locale_dir/locale_base_api/newlib.h. It appears to conditionally include a number of headers that provide a substitute no-op implementation of the POSIX locale interfaces for the newlib C library. Perhaps you can modify include/__locale_dir/locale_base_api/musl.h to do similarly.

I borrowd the definitions for ‘strtof_l’, ‘strtod_l’, and ‘strtold_l’ from __support/xlocale/__strtonum_fallback.h. I used the definition for locale_t from musl’s locale.h by defining the macro __NEED_locale_t.
I am now able to build libc++.a with locale support.
I have tested this, and now I can build a simple c++ testcase that includes iostream successfully.

Thanks for the helpful suggestion!

I’m glad to hear that. Assuming your changes would be applicable to ~all users of libc++ with the musl C library, please submit a pull request with those changes!

Sure.
I have not forgotten this. I will get to it hopefully soon.

jhuber6 July 25, 2024, 4:12pm 10

I’d appreciate it as well, seeing as locales are one of the few remaining issues preventing us from building libc++ on top of LLVM libc.