gpt4 book ai didi

c++ - Linux 中奇怪的 iconv_open 行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:56:08 24 4
gpt4 key购买 nike

在将大型应用程序从 Windows 移植到 Linux 时,我需要能够在宽字符和多字节字符之间进行转换。为此,我有如下代码:

void IConv(const InType* begin, const InType* end, const char* inCode, OutType* outBegin,  OutType*& outEnd, const char* outCode)
{
assert(end >= begin);
assert(outEnd > outBegin);

iconv_t cd = iconv_open(outCode, inCode);
if (cd == reinterpret_cast<iconv_t>(-1))
throw (InvalidLocale ());
/* blah, blah, blah other code we never reach */
}

该代码总是抛出异常。为了对此进行调试,我创建了一个更简单的版本,它使用与失败代码相同的参数。这是我的测试代码

int main( void )
{

const char outCode[] = "";
const char inCode[] = "wchar_t";

//Using wchar_t and "" means that iconv will just use the system locale settings.
iconv_t cd = iconv_open(outCode, inCode);
if (cd == reinterpret_cast<iconv_t>(-1))
{
printf("iconv failed to use outCode %s and inCode %s\n",outCode, inCode);
return 1;
}

iconv_close(cd);
return 0;
}

请注意,代码几乎相同。但在我的测试代码中,我从未见过失败,而 IConv 函数总是失败。系统上的语言环境是通过 LANG 环境变量设置的,在本例中始终为 ISO-8859-1。

所以,问题是,有没有人知道 iconv 中的任何特定行为可能会出现在大型应用程序中,但不会出现在简单的情况下?谢谢

最佳答案

问题很可能是目标机器没有安装合适的 iconv 库和索引。请参阅 /usr/lib[64]/gconv。共享库通常是 glibc 安装的一部分。 localedef 等工具可以创建它们。

关于c++ - Linux 中奇怪的 iconv_open 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9560671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com