一开始,调用iconv_open()会返回-1,使用printf("errno=%d\n",errno)查看,返回22,说是invalid argument参数非法。
试了下,确定不是UTF-8或utf-8的大小写问题。然后按博客http://blog.csdn.net/love_life2011/article/details/7086910#comments所述,更新iconv函数簇为较新版本后即可。
附http://blog.csdn.net/love_life2011/article/details/7086910#comments内容:
相同的C源程序,在ubuntu11.10上能够正常执行,而在ARM开发板则不能正常执行,调用
iconv_open("utf-8", "gb2312") 返回失败,错误信息为“Invalid argument”.
经过查询资料得知iconv相关函数为libc中的函数,初步分析得出结论为有可能是libc版本中
iconv相关函数的版本不同造成的,因此要更新iconv相关函数。
更新iconv相关函数有两种方法:
第一,更新libc库;
第二,更新libiconv库。
第一种方法更新libc库比较麻烦,因为我们用的是编译好的交叉编译器,这中方法需要重新编译生成
交叉编译器,并且也需要使用新编译生成的交叉编译工具重新编译应用程序,因此本方法代价太大,
采用第二种方法。
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
然后再执行应用程序即可,正常运行。