ARM开发板上iconv_open(“utf-8", "gb2312”) 调用失败的解决

赵丹icons逐LJ 2012-01-09

ARM开发板上iconv_open("utf-8", "gb2312") 调用失败的解决方法

应用程序代码如下:
static int code_convert(char* from_charset, char* to_charset,
        char* inbuf, size_t inlen, char* outbuf, size_t outlen)
{
    iconv_t cd;
    char **pin = &inbuf;
    char **pout = &outbuf;

    cd = iconv_open(to_charset, from_charset);
    if (cd == -1)
    {
        perror("iconv_open:");
        return -1;
    }

    memset(outbuf,0,outlen);
    if (iconv(cd, pin, &inlen, pout, &outlen) == -1)
    {
        //printf("%s: call iconv failed!\n", __FUNCTION__);
        printf("errno=%d\n", errno);
        perror("iconv failed:\n");
        iconv_close(cd);
        return -1;
    }
    iconv_close(cd);
    return 0;
}

static int g2u(char* inbuf, size_t inlen, char* outbuf, size_t outlen)
{
    return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
}

开发环境为Ubuntu11.10,开发板为ARM开发板,交叉编译器版本为arm-linux-4.4.3.
相同的C源程序,在ubuntu11.10上能够正常执行,而在ARM开发板则不能正常执行,调用
iconv_open("utf-8", "gb2312") 返回失败,错误信息为“Invalid argument”.
经过查询资料得知iconv相关函数为libc中的函数,初步分析得出结论为有可能是libc版本中
iconv相关函数的版本不同造成的,因此要更新iconv相关函数。
更新iconv相关函数有两种方法:
第一,更新libc库;
第二,更新libiconv库。

第一种方法更新libc库比较麻烦,因为我们用的是编译好的交叉编译器,这中方法需要重新编译生成

交叉编译器,并且也需要使用新编译生成的交叉编译工具重新编译应用程序,因此本方法代价太大,

采用第二种方法。


第二种方法为只更新libiconv库,到iconv官网下载最新的库源码包,下载地址为:
http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz
解压后得到libiconv目录,阅读该目录下Readme文件得出,编译安装libiconv库也有两种方式:
This library can be built and installed in two variants:

  - The library mode. This works on all systems, and uses a library
    `libiconv.so' and a header file `<iconv.h>'. (Both are installed
    through "make install".)

    To use it, simply #include <iconv.h> and use the functions.

    To use it in an autoconfiguring package:
    - If you don't use automake, append m4/iconv.m4 to your aclocal.m4
      file.
    - If you do use automake, add m4/iconv.m4 to your m4 macro repository.
    - Add to the link command line of libraries and executables that use
      the functions the placeholder @LIBICONV@ (or, if using libtool for
      the link, @LTLIBICONV@). If you use automake, the right place for
      these additions are the *_LDADD variables.
    Note that 'iconv.m4' is also part of the GNU gettext package, which
    installs it in /usr/local/share/aclocal/iconv.m4.

  - The libc plug/override mode. This works on GNU/Linux, Solaris and OSF/1
    systems only. It is a way to get good iconv support without having
    glibc-2.1.
    It installs a library `preloadable_libiconv.so'. This library can be used
    with LD_PRELOAD, to override the iconv* functions present in the C library.

    On GNU/Linux and Solaris:
        $ export LD_PRELOAD=/usr/local/lib/preloadable_libiconv.so

    On OSF/1:
        $ export _RLD_LIST=/usr/local/lib/preloadable_libiconv.so:DEFAULT

    A program's source need not be modified, the program need not even be
    recompiled. Just set the LD_PRELOAD environment variable, that's it!

我在ARM开发板上采用“The libc plug/override mode”实验成功,下面介绍编译过程:
在libiconv目录下:
$./configure --prefix=$PWD/out --host=arm-linux
$make
$make install

上述命令执行完成后会在libiconv目录下生成新的out目录,该目录下存在4个目录分别是:
bin include lib share

在lib目录下为生成的库文件,其中一个为preloadable_libiconv.so,把它下载到开发板
的lib目录下,然后再设置开发板的系统环境变量:
$ export LD_PRELOAD=/lib/preloadable_libiconv.so

然后再执行应用程序即可,正常运行。

相关推荐

Chenliaoyuan / 0评论 2019-10-28