linux iconv函数失败,Linux 编码转换 (iconv失败的解决方法)
一開(kāi)始,調(diào)用iconv_open()會(huì)返回-1,使用printf("errno=%d\n",errno)查看,返回22,說(shuō)是invalid argument參數(shù)非法。
試了下,確定不是UTF-8或utf-8的大小寫(xiě)問(wèn)題。然后按博客http://blog.csdn.net/love_life2011/article/details/7086910#comments所述,更新iconv函數(shù)簇為較新版本后即可。
附http://blog.csdn.net/love_life2011/article/details/7086910#comments內(nèi)容:
相同的C源程序,在ubuntu11.10上能夠正常執(zhí)行,而在ARM開(kāi)發(fā)板則不能正常執(zhí)行,調(diào)用 iconv_open("utf-8", "gb2312") 返回失敗,錯(cuò)誤信息為“Invalid argument”. 經(jīng)過(guò)查詢(xún)資料得知iconv相關(guān)函數(shù)為libc中的函數(shù),初步分析得出結(jié)論為有可能是libc版本中 iconv相關(guān)函數(shù)的版本不同造成的,因此要更新iconv相關(guān)函數(shù)。 更新iconv相關(guān)函數(shù)有兩種方法: 第一,更新libc庫(kù); 第二,更新libiconv庫(kù)。 第一種方法更新libc庫(kù)比較麻煩,因?yàn)槲覀冇玫氖蔷幾g好的交叉編譯器,這中方法需要重新編譯生成
交叉編譯器,并且也需要使用新編譯生成的交叉編譯工具重新編譯應(yīng)用程序,因此本方法代價(jià)太大,
采用第二種方法。
第二種方法為只更新libiconv庫(kù),到iconv官網(wǎng)下載最新的庫(kù)源碼包,下載地址為:
http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz
解壓后得到libiconv目錄,閱讀該目錄下Readme文件得出,編譯安裝libiconv庫(kù)也有兩種方式:
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開(kāi)發(fā)板上采用“The libc plug/override mode”實(shí)驗(yàn)成功,下面介紹編譯過(guò)程:
在libiconv目錄下:
$./configure --prefix=$PWD/out --host=arm-linux
$make
$make install
上述命令執(zhí)行完成后會(huì)在libiconv目錄下生成新的out目錄,該目錄下存在4個(gè)目錄分別是:
bin include lib share
在lib目錄下為生成的庫(kù)文件,其中一個(gè)為preloadable_libiconv.so,把它下載到開(kāi)發(fā)板
的lib目錄下,然后再設(shè)置開(kāi)發(fā)板的系統(tǒng)環(huán)境變量:
$ export LD_PRELOAD=/lib/preloadable_libiconv.so
然后再執(zhí)行應(yīng)用程序即可,正常運(yùn)行。
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的linux iconv函数失败,Linux 编码转换 (iconv失败的解决方法)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux下编译cscope,vim+c
- 下一篇: linux打开sqlite3数据库,Ce