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

标签 c++ iconv libiconv

在将大型应用程序从 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/

相关文章:

c++ - 在 qt whit C++ 代码中不显示汇编代码

c++ - 回顾C++编译时间计数器

Linux find重新编码所有文件到子目录

mingw - 如何使用 msys 在 Windows 上静态链接 iconv?

ruby - libiconv 在 OSX 10.8 安装 nokogiri 时丢失

c++ - 将 VAO+IBO 与 GLSL 着色器结合使用

c++ - GetAcceptExSockaddrs 填充的指针的生命周期

r - 强制将编码从未知设置为 UTF-8 或 R 中的任何编码?

c - 为什么作为命令行参数传入的 UTF-16 字符串的 hexdump 与直接在终端上的不同?