【发布时间】:2017-10-11 11:05:20
【问题描述】:
我想用 c++ 中的 python 代码实现相同的结果:
import locale
encoding = locale.getpreferredencoding()
encoding 在我的电脑上是“cp936”(windows 10,简体中文)
我尝试以下 C++ 代码:
test1.cpp:
#include <iostream>
#include <clocale>
using namespace std;
int main()
{
setlocale(LC_ALL, "");
string lc_all = std::setlocale(LC_ALL, NULL);
cout << lc_all << endl; // prints Chinese (Simplified)_China.936
return 0;
}
嗯...我还是不满意,所以我转向boost::locale(boost_1_65_1),但是有一些奇怪的行为。
test2.cpp:
#include <iostream>
#include <boost/locale.hpp>
using namespace std;
int main()
{
boost::locale::generator gen;
string encoding = std::use_facet<boost::locale::info>(gen("")).encoding();
cout << encoding << endl;
return 0;
}
问题一:代码会导致链接错误,除非包含另一个标题:#include <boost/filesystem.hpp>,为什么?有什么文件可以说要使用boost::locale 还必须包含boost::filesystem?
1>libboost_locale-vc140-mt-1_65_1.lib(generator.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ)
1>libboost_locale-vc140-mt-1_65_1.lib(date_time.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ)
1>libboost_locale-vc140-mt-1_65_1.lib(localization_backend.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ)
1>libboost_locale-vc140-mt-1_65_1.lib(lcid.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ)
1>libboost_locale-vc140-mt-1_65_1.lib(generator.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ)
1>libboost_locale-vc140-mt-1_65_1.lib(date_time.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ)
1>libboost_locale-vc140-mt-1_65_1.lib(localization_backend.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ)
1>libboost_locale-vc140-mt-1_65_1.lib(lcid.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ)
问题2:添加#include <boost/filesystem.hpp>,程序运行,但是encoding是“utf-8”,为什么?我以为是'cp936',和test1.cpp
问题:用 c++ 获取语言环境编码的最佳方法是什么(使用 python 的 locale.getpreferredencoding() 实现相同的结果)。非常感谢!
环境: win10 64bit, vs2015, boost_1_65_1(windows pre-build, lib64-msvc-14.0)
【问题讨论】:
-
先阅读文档!
-
@Silencer 你怎么看我没读过文档?您可以给出更有用的评论,例如所谓的“文档”的正确位置
-
它显示是因为boost.org/doc/libs/1_62_0/libs/locale/doc/html/examples.html 准确显示了您需要执行的操作。
-
@UKMonkey 谢谢,但我的问题没有解决方案。在提出问题之前,我已经搜索了两天。
-
我不太明白你的问题。我无法猜测您是要获取还是设置编码。我真的不想在Win X上折腾。但是官方文档或CSDN可能会帮助您对“CP936”进行编码。 blog.csdn.net/aseity/article/details/48313099