带有 LANG=en_US.UTF-8 的 MacOS 10.6 上的 std::locale 损坏

std::locale breakage on MacOS 10.6 with LANG=en_US.UTF-8(带有 LANG=en_US.UTF-8 的 MacOS 10.6 上的 std::locale 损坏)

本文介绍了带有 LANG=en_US.UTF-8 的 MacOS 10.6 上的 std::locale 损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C++ 应用程序,我要移植到 MacOSX(特别是 10.6).该应用程序大量使用 C++ 标准库和 boost.我最近发现应用中有一些我难以理解的损坏.

I have a C++ application that I am porting to MacOSX (specifically, 10.6). The app makes heavy use of the C++ standard library and boost. I recently observed some breakage in the app that I'm having difficulty understanding.

基本上,boost 文件系统库在程序运行时会抛出运行时异常.通过一些调试和谷歌搜索,我将有问题的调用减少到以下最小程序:

Basically, the boost filesystem library throws a runtime exception when the program runs. With a bit of debugging and googling, I've reduced the offending call to the following minimal program:

#include <locale>

int main ( int argc, char *argv [] ) {
    std::locale::global(std::locale(""));
    return 0;
}

当我通过 g++ 运行该程序并在设置了 LANG=en_US.UTF-8 的环境中执行生成的程序(在我的计算机上是默认 bash 会话的一部分,当我创建了一个新的控制台窗口).清除环境变量 (setenv LANG=) 使程序可以毫无问题地运行.但我很惊讶我在默认配置中看到这种损坏.

This program fails when I run this through g++ and execute the resulting program in an environment where LANG=en_US.UTF-8 is set (which on my computer is part of the default bash session when I create a new console window). Clearing the environment variable (setenv LANG=) allows the program to run without issues. But I'm surprised I'm seeing this breakage in the default configuration.

我的问题是:

  1. 此代码在 MacOS 10.6 上的预期行为是否如此?
  2. 正确的解决方法是什么?我无法真正重写该函数,因为我们使用的 boost 库版本在内部执行此语句作为文件系统库的一部分.

为了完整起见,我应该指出合成此代码的程序在通过打开"命令(或从 Finder)启动时崩溃,但在 Xcode 以调试模式运行程序时不会崩溃.

For completeness, I should point out that the program from which this code was synthesized crashes when launched via the 'open' command (or from the Finder) but not when Xcode runs the program in Debug mode.

edit 上面代码在10.6.1上给出的​​错误是:

edit The error given by the above code on 10.6.1 is:

$ ./locale 
terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid
Abort trap

推荐答案

我最近在 Ubuntu 14.04 LTS 和运行最新 Raspbian Wheezy 的 Raspberry Pi 上遇到了这个问题.

I have encountered this problem very recently on Ubuntu 14.04 LTS and on a Raspberry Pi running the latest Raspbian Wheezy.

它与 OS X 无关,而是与 G++ 和 Boost(至少到 V1.55)以及某些平台上的默认区域设置的组合.有与此问题相关的 Boost 错误票,请参阅票 #4688 和 票号 #5928.

It has nothing to do with OS X, rather with a combination of G++ and Boost (at least up to V1.55) and the default locale settings on certain platforms. There are Boost bug tickets sort of related to this issue, see ticket #4688 and ticket #5928.

我的解决方案"首先进行了一些额外的语言环境设置,正如 这个 AskUbuntu 帖子:

My "solution" was first to do some extra locale setup, as suggested by this AskUbuntu posting:

sudo locale-gen en_US en_US.UTF-8
sudo dpkg-reconfigure locales

但是,我还必须确保将环境变量 LC_ALL 设置为 LANG 的值(建议将其放入您的 .profile):

But then, I also had to make sure that the environment variable LC_ALL is set to the value of LANG (it is advisable to put this in your .profile):

export LC_ALL=$LANG

就我而言,我使用区域设置 en_US.UTF-8.

In my case I use the locale en_US.UTF-8.

最后一句话:OP 说当我通过 g++ 运行这个程序时,这个程序失败了".我知道这个线程是在 2009 年开始的,但是今天 绝对不需要在 Mac 上使用 GCC 或 G++,Apple 免费提供更好的 LLVM/Clang 编译器套件,请参阅XCode 主页.

Final remark: the OP said "This program fails when I run this through g++". I understand that this thread was started in 2009, but today there is absolutely no need to use GCC or G++ on the Mac, the much better LLVM/Clang compiler suite is available from Apple free of charge, see the XCode home page.

这篇关于带有 LANG=en_US.UTF-8 的 MacOS 10.6 上的 std::locale 损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有 LANG=en_US.UTF-8 的 MacOS 10.6 上的 std::locale 损坏

基础教程推荐