Linux中奇怪的iconv_open行为

Weird iconv_open behavior in Linux

本文关键字:open 行为 iconv Linux      更新时间:2023-10-16

在将大型应用程序从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 %sn",outCode, inCode);
        return 1;
    }
    iconv_close(cd);
    return 0;
}

请注意,代码基本相同。但在我的测试代码中,我从未看到过失败,而IConv函数总是失败。系统上的区域设置是通过LANG-env变量设置的,在本例中该变量始终为ISO-8859-1。

所以,问题是,有人知道iconv中的任何特定行为可能会出现在一个大型应用程序中,但不是在一个简单的案例中吗?感谢

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