-fshort-wchar 和 std::wstring - 分段错误

-fshort-wchar and std::wstring - segmentation fault

我使用 Ubuntu 18.04 和 gcc 7.5.0。我有一个用 -fshort-wchar 编译的库。我注意到在库中使用 std::wstring 可能会导致分段错误。我找到了一个类似的topic。有解释:

-fshort-wchar is not usable if you want to interact with any part of the standard library or third-party library code using the correct (32-bit) definition of wchar_t.

我知道当使用 -fshort-wchar 编译库时我不能使用 std::wstring 但是其他标准库内容呢?为什么我不能与标准库的任何部分交互,例如 std::vector ?

-fshort-wchar          sets the size of wchar_t to 2 bytes

现在想一想如果您只使用 而不是 header 的标准库的一部分会发生什么。标准库的很大一部分被编译成一个库,你 link with (libstdc++ / libc++)。编译时 -fshort-wchar 未被使用,因此 wchar_t 的大小在库中为 4 个字节。