设为首页 加入收藏

TOP

ARM开发板上iconv_open(“utf-8", "gb2312”) 调用失败的解决方法
2014-11-24 14:37:01 来源: 作者: 【 】 浏览:10
Tags:ARM 开发 iconv_open utf-8 gb2312 调用 失败 解决 方法

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);
}


第一种方法更新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 `'. (Both are installed
through "make install".)


To use it, simply #include 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


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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇编译运行在omap4430平台的Android.. 下一篇Android 网页登录 POST 请求 保存..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: