我想要实现在C蟒蛇码++相同的结果:C++得到了用户首选编码或本地编码
import locale
encoding = locale.getpreferredencoding()
的encoding
是我的电脑“CP936”(窗口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),但有一些奇怪的行为。
测试2.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;
}
问题1:的代码将导致连接错误,除非包括另一个头:#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)" ([email protected]@[email protected]@[email protected]@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)" ([email protected]@[email protected]@[email protected]@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)" ([email protected]@[email protected]@[email protected]@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)" ([email protected]@[email protected]@[email protected]@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)" ([email protected]@[email protected]@[email protected]@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)" ([email protected]@[email protected]@[email protected]@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)" ([email protected]@[email protected]@[email protected]@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)" ([email protected]@[email protected]@[email protected]@XZ)
问题2:通过添加#include <boost/filesystem.hpp>
,程序运行,但encoding
是 “UTF-8”,为什么呢?我认为这是“CP936”,与test1.cpp
问题的结果是一致的:什么++获得的C语言环境编码(实现与Python的locale.getpreferredencoding()
相同的结果)最好的主意。非常感谢!
环境: win10 64位,vs2015,boost_1_65_1(Windows预编译,lib64下-MSVC-14.0)
先阅读文档! – Silencer
@Silencer你怎么看我没看过这个文档?您可以提供更有用的评论,例如所谓的“文档”的正确位置 – oz1
由于http://www.boost.org/doc/libs/1_62_0/libs/locale/doc/html/examples .html显示你需要做的事情。 – UKMonkey