【发布时间】:2013-12-04 23:46:00
【问题描述】:
我似乎对 C++ 中的语言环境有疑问。当我从 Eclipse 中运行我的程序时,一切正常。但是,当我尝试从命令行运行时,我不断收到此错误:
失败:locale::facet::_S_create_c_locale 名称无效
这是触发错误的代码:
// Set up UTF8 file stream
string fileName = "./sz.txt";
wifstream inFileStream;
try {
setlocale(LC_ALL, "");
inFileStream.open(fileName.c_str());
inFileStream.imbue(locale(""));
if(!inFileStream) {
return EXIT_FAILURE;
}
}
catch (const std::exception &exc) {
wcout << "Error while trying to create UTF8 file stream." << endl;
std::cerr << exc.what() << endl;
if( inFileStream.is_open() )
inFileStream.close();
return EXIT_FAILURE;
}
“locale”的输出如下:
LANG="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_CTYPE="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_ALL="de_DE.UTF-8"
我也尝试使用“de_DE.UTF-8”作为语言环境字符串而不是“”(实际上应该是这样),但这给了我同样的错误。
奇怪的是,当在 Eclipse 中运行时,该程序运行良好。我正在使用 g++ 从命令行编译以下版本:
配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM 5.0 版(clang-500.2.79)(基于 LLVM 3.3svn) 目标:x86_64-apple-darwin12.4.0 线程模型:posix
知道这里可能有什么问题吗?
干杯,
马丁
【问题讨论】:
标签: c++ utf-8 g++ locale libstdc++