The following code works fine on Linux but throws an exception on OS X 10.7:
以下代码在Linux上运行良好,但在OS X 10.7上抛出异常:
#include <iostream>
#include <locale>
#include <stdexcept>
int main() try {
std::locale::global(std::locale(""));
std::cout << "Using locale: " << std::locale().name() << "\n";
}
catch (std::runtime_error const& e) {
std::cout << e.what() << "\n";
return 1;
}
The output on OS X is:
OS X的输出是:
locale::facet::_S_create_c_locale
name not valid地区::方面::_S_create_c_locale名称无效
However, the standard explicitly says that
然而,该标准明确指出了这一点
The set of valid string argument values is
"C"
,""
, and any implementation-defined values.有效的字符串参数值集是“C”、“”和任何实现定义的值。
So whatever causes the behaviour above is violating the standard.
所以无论什么原因导致上述行为都违反了标准。
The compiler used is clang++ 3.1 (tags/Apple/clang-318.0.58); I’ve also tried it with GCC 4.7, installed via Homebrew, with the same result.
使用的编译器为clang++ 3.1 (tags/Apple/clang-318.0.58);我也尝试了GCC 4.7,通过Homebrew安装,结果相同。
Can other people validate this problem? What causes it? Am I doing anything wrong? Is this a bug in OS X?
其他人能证实这个问题吗?导致它的原因是什么?我做错什么了吗?这是OS X中的错误吗?
(Maybe this relates to another xlocale
problem but the errors are actually completely different.)
(这可能与另一个xlocale问题有关,但错误实际上是完全不同的。)
2 个解决方案
#1
2
I don't think you're using xlocale. I believe that your problem is with libstdc++, which uses a different locale support library that is not supported on OS X, as the question EitanT links to states.
我不认为你在使用xlocale。我认为您的问题是libstdc++,它使用OS X不支持的另一个语言环境支持库,作为问题EitanT链接到状态。
If you switch to libc++ your program will work.
如果您切换到libc+,您的程序将会正常工作。
#2
0
The poster above it correct...the problem is with libstdc++. I wanted to add my answer because it is not straightforward how to get OS X to link against libc++ and took me over an hour to figure out.
上面的海报是正确的……问题是libstdc++。我想添加我的答案,因为如何让OS X链接到libc++ +并花了我一个多小时才弄明白。
Invoking the compiler/linker by g++ -libstd=libc++
or by clang++ -libstd=libc++
or by the alias c++ -libstd=libc++
will all fail.
通过g+ -libstd=libc+或clang++ -libstd=libc++ +或别名c++调用编译器/链接器都将失败。
The solution for compiling simple programs from the command line instead of messing with the added overhead of Xcode is to allow Xcode to handle the linking by using the command xcrun clang++ -stdlib=libc++
从命令行编译简单程序而不是使用Xcode的额外开销的解决方案是允许Xcode使用命令xcrun clang+ -stdlib=libc++来处理链接。
xcrun
allows Xcode to manage the tool chain and will build a successful executable where cout.imbue(locale(foo))
will successfully work.
xcrun允许Xcode管理工具链,并将构建一个成功的可执行文件,其中cout.imbue(locale(foo))将成功地工作。
#1
2
I don't think you're using xlocale. I believe that your problem is with libstdc++, which uses a different locale support library that is not supported on OS X, as the question EitanT links to states.
我不认为你在使用xlocale。我认为您的问题是libstdc++,它使用OS X不支持的另一个语言环境支持库,作为问题EitanT链接到状态。
If you switch to libc++ your program will work.
如果您切换到libc+,您的程序将会正常工作。
#2
0
The poster above it correct...the problem is with libstdc++. I wanted to add my answer because it is not straightforward how to get OS X to link against libc++ and took me over an hour to figure out.
上面的海报是正确的……问题是libstdc++。我想添加我的答案,因为如何让OS X链接到libc++ +并花了我一个多小时才弄明白。
Invoking the compiler/linker by g++ -libstd=libc++
or by clang++ -libstd=libc++
or by the alias c++ -libstd=libc++
will all fail.
通过g+ -libstd=libc+或clang++ -libstd=libc++ +或别名c++调用编译器/链接器都将失败。
The solution for compiling simple programs from the command line instead of messing with the added overhead of Xcode is to allow Xcode to handle the linking by using the command xcrun clang++ -stdlib=libc++
从命令行编译简单程序而不是使用Xcode的额外开销的解决方案是允许Xcode使用命令xcrun clang+ -stdlib=libc++来处理链接。
xcrun
allows Xcode to manage the tool chain and will build a successful executable where cout.imbue(locale(foo))
will successfully work.
xcrun允许Xcode管理工具链,并将构建一个成功的可执行文件,其中cout.imbue(locale(foo))将成功地工作。