<div dir="ltr"><div><div><div>Hi,<br></div>I'm trying to build libc++ against musl-libc ( <a href="http://www.musl-libc.org">www.musl-libc.org</a> ), another linux C library. I have successfully built a musl-libc based C toolchain, a freestanding C++ compiler, and some C++ runtime libraries like libcxxrt, libunwind, I even succeeded in building libc++ against musl-libc, but a simple hello-world program won't run.<br>
<br></div><div>the following code would throw a bad_cast exception:<br><br>#include <locale><br>#include <iostream><br>#include <cstdio><br><br>using namespace std;<br><br>int main() {<br>    locale loc("C");<br>
    //cout.imbue(loc);  // If this line is added, the whole program runs well.<br>    cout << "hello" << endl;<br>    return 0;<br>}<br><br>This is the call stack of the thrown exception:<br>#6  0x00000000004ce2d9 in __cxa_throw ()<br>
#7  0x0000000000422a40 in std::__1::locale::__imp::use_facet(long) const ()<br>    at /home/hoolala/workspace/bootcxx/out/libcxx/src/locale.cpp:439<br>#8  0x0000000000423379 in std::__1::locale::use_facet(std::__1::locale::id&) const ()<br>
    at /home/hoolala/workspace/bootcxx/out/libcxx/src/locale.cpp:581<br>#9  0x00000000004005ad in std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<< <std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) ()<br>
    at /home/hoolala/workspace/bootcxx/out/sysroot/usr/include/c++/v1/__locale:174<br>#10 0x000000000040035a in main () at loc.cpp:12<br><br></div><div></div>I applied a simple patch to the original libc++ code:<br><br>Index: include/__locale<br>
===================================================================<br>--- include/__locale    (revision 190843)<br>+++ include/__locale    (working copy)<br>@@ -376,7 +376,7 @@<br>     static const mask xdigit = _ISXDIGIT;<br>
     static const mask blank  = _ISBLANK;<br> #else  // __GLIBC__ || _WIN32 || __APPLE__ || __FreeBSD__ || EMSCRIPTEN || __sun__<br>-    typedef unsigned long mask;<br>+    typedef unsigned short mask;<br>     static const mask space  = 1<<0;<br>
     static const mask print  = 1<<1;<br>     static const mask cntrl  = 1<<2;<br>Index: include/locale<br>===================================================================<br>--- include/locale    (revision 190843)<br>
+++ include/locale    (working copy)<br>@@ -829,7 +829,7 @@<br>         typename remove_reference<decltype(errno)>::type __save_errno = errno;<br>         errno = 0;<br>         char *__p2;<br>-        long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);<br>
+        long long __ll = strtoll(__a, &__p2, __base);<br>         typename remove_reference<decltype(errno)>::type __current_errno = errno;<br>         if (__current_errno == 0)<br>             errno = __save_errno;<br>
@@ -869,7 +869,7 @@<br>         typename remove_reference<decltype(errno)>::type __save_errno = errno;<br>         errno = 0;<br>         char *__p2;<br>-        unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);<br>
+        unsigned long long __ll = strtoull(__a, &__p2, __base);<br>         typename remove_reference<decltype(errno)>::type __current_errno = errno;<br>         if (__current_errno == 0)<br>             errno = __save_errno;<br>
@@ -899,7 +899,7 @@<br>         typename remove_reference<decltype(errno)>::type __save_errno = errno;<br>         errno = 0;<br>         char *__p2;<br>-        long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);<br>
+        long double __ld = strtold(__a, &__p2);<br>         typename remove_reference<decltype(errno)>::type __current_errno = errno;<br>         if (__current_errno == 0)<br>             errno = __save_errno;<br>
Index: src/locale.cpp<br>===================================================================<br>--- src/locale.cpp    (revision 190843)<br>+++ src/locale.cpp    (working copy)<br>@@ -1005,6 +1005,8 @@<br> extern "C" const int ** __ctype_toupper_loc();<br>
 #endif<br> <br>+extern "C" const unsigned short ** __ctype_b_loc();<br>+<br> const ctype<char>::mask*<br> ctype<char>::classic_table()  _NOEXCEPT<br> {<br>@@ -1027,10 +1029,11 @@<br> #else<br>     // Platform not supported: abort so the person doing the port knows what to<br>
     // fix<br>-# warning  ctype<char>::classic_table() is not implemented<br>-    printf("ctype<char>::classic_table() is not implemented\n");<br>-    abort();<br>-    return NULL;<br>+//# warning  ctype<char>::classic_table() is not implemented<br>
+    //printf("ctype<char>::classic_table() is not implemented\n");<br>+    //abort();<br>+    //return NULL;<br>+    return *__ctype_b_loc();<br> #endif<br> }<br><br></div>Did I miss anything in the patch ?<br>
<br>Thanks<br>Yao<br><div><div> <br></div></div></div>