Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libstdc++ features like stacktrace, text_encoding, time zone and locale are not working correctly #250

Open
Shuangcheng-Ni opened this issue Jan 8, 2025 · 1 comment

Comments

@Shuangcheng-Ni
Copy link

stacktrace & text_encoding

stacktrace and text_encoding are not enabled by default.

PS > g++ -std=c++26 -x c++ - -lstdc++exp
#include <stacktrace>
#include <text_encoding>

int main()
{
    std::stacktrace::current();
    std::text_encoding::environment();
}
^Z
<stdin>: In function 'int main()':
<stdin>:6:10: error: 'std::stacktrace' has not been declared
<stdin>:7:10: error: 'std::text_encoding' has not been declared

If I force enable them, I will get link errors.

PS > g++ -std=c++26 -x c++ - -lstdc++exp
#define __cpp_lib_stacktrace
#define __cpp_lib_text_encoding
#include <stacktrace>
#include <text_encoding>

int main()
{
    std::stacktrace::current();
    std::text_encoding::environment();
}
^Z
.../mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\...\AppData\Local\Temp\ccuMXIMp.o:<stdin>:(.text+0x3f): undefined reference to `std::text_encoding::environment()'
.../mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\...\AppData\Local\Temp\ccuMXIMp.o:<stdin>:(.text$_ZNSt16basic_stacktraceISaISt16stacktrace_entryEE7currentERKS1_[_ZNSt16basic_stacktraceISaISt16stacktrace_entryEE7currentERKS1_]+0x59): undefined reference to `std::__stacktrace_impl::_S_current(int (*)(void*, unsigned long long), void*, int)'
collect2.exe: error: ld returned 1 exit status

The error is caused by the lack of related functions in libstdc++exp.a.

PS > nm .\mingw64\lib\libstdc++exp.a | sls 'stacktrace.o|text_encoding.o' -Context 0, 5

> stacktrace.o:
  0000000000000000 b .bss
  0000000000000000 d .data
  0000000000000000 r .rdata$zzz
  0000000000000000 t .text

> text_encoding.o:
  0000000000000000 b .bss
  0000000000000000 d .data
  0000000000000000 r .rdata$zzz
  0000000000000000 t .text

time zone & locale

std::chrono::current_zone() from libstdc++ always returns UTC, while its C-style equivalent std::localtime from ucrt/msvcrt works correctly.

PS > g++ -std=c++26 -x c++ - -lstdc++exp
#include <chrono>
#include <iostream>
#include <print>

using namespace std::literals;

int main()
{
    std::chrono::sys_days   tp{ 2025y / 1 / 1 };
    std::chrono::zoned_time zt(std::chrono::current_zone(), tp);
    std::println("{:%F %T %z (%Z)}", zt);

    auto t{ std::chrono::system_clock::to_time_t(tp) };
    std::cout << std::put_time(std::localtime(&t), "%F %T %z (%Z)") << std::endl;
}
^Z

PS > .\a.exe
2025-01-01 00:00:00 +0000 (UTC)
2024-12-31 18:00:00 -0600 (中部标准时间)

std::locale from libstdc++ only accepts nullptr, C and POSIX, while its C-style equivalent std::setlocale from ucrt/msvcrt works correctly.

PS > g++ -std=c++26 -x c++ - -lstdc++exp
#include <iostream>
#include <locale>
#include <print>

int main()
{
    constexpr std::wstring_view s{ L"aα啊" };

    const char *locales[]{ nullptr, "C", "POSIX", "", "zh_CN.UTF-8" };

    for (auto locale : locales)
    {
        std::println("locale: \"{}\"", locale ? locale : "nullptr");

        std::println("C++");
        try
        {
            std::locale::global(locale ? std::locale(locale) : std::locale{});
            std::wcout << s << std::endl;
        }
        catch (std::exception &e)
        {
            std::println("exception: {}", e.what());
        }
        std::println("wcout.good(): {}", std::wcout.good());
        std::wcout.clear();

        std::println("C");
        std::setlocale(LC_ALL, locale);
        std::wcout << s << std::endl;
        std::println("wcout.good(): {}", std::wcout.good());
        std::wcout.clear();

        std::println();
    }
}
^Z

PS > .\a.exe
locale: "nullptr"
C++
awcout.good(): false
C
awcout.good(): false

locale: "C"
C++
awcout.good(): false
C
awcout.good(): false

locale: "POSIX"
C++
awcout.good(): false
C
awcout.good(): false

locale: ""
C++
exception: locale::facet::_S_create_c_locale name not valid
wcout.good(): true
C
aα啊
wcout.good(): true

locale: "zh_CN.UTF-8"
C++
exception: locale::facet::_S_create_c_locale name not valid
wcout.good(): true
C
aα啊
wcout.good(): true

Linux side status

All these features work correctly on Linux side. These issues are MinGW-only.

@brechtsanders
Copy link
Owner

I'm not sure how I can help here as I just build GCC+MinGW-w64.

The best way forward is probably to report these issues in separate bug reports to the GCC and/or MinGW-w64 developers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants