LibIConv库裁剪

原文地址http://blog.chinaunix.net/uid-25885064-id-3181444.html

编译libiconv库之后,发现库文件有1M多,经过压缩之后还是有600~700K,对于flash很小的嵌入式系统来说,简直就是致命的,而且该库中的很多编码方式并不是我们所需要的。如果自己重新写编码转换的话,又很耗时间。个人认为裁减libiconv库是一个不错的选择,google了一下,发现根本没有关于裁减这个库的资料。所以,尝试着作了裁减。以下是我操作的相关步骤,供有需要的朋友参考。


1、从官网上下载libiconv库,最新版本的下载地址:http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz

2、解压下载的库文件
# tar -xzvf libiconv-1.14.tar.gz
3、修改相应的三个文件(converters.h,encodings.def,aliases.h)即可,都位于libiconv-1.14/lib目录下,以下以删减iso2022_kr编码库为例:
a.去除converters.h中定义的头文件
#if 0
#include "iso2022_kr.h"
#endif

在终端执行./configure & make,出现以下错误
make[1]: Entering directory `/root/example/libiconv-1.14/lib'
/bin/sh ../libtool --mode=compile gcc -I. -I. -I../include -I./../include -I.. -I./.. -g -O2 -fvisibility=hidden -DLIBDIR=\"/usr/local/lib\" -DBUILDING_LIBICONV -DBUILDING_DLL -DENABLE_RELOCATABLE=1 -DIN_LIBRARY -DINSTALLDIR=\"/usr/local/lib\" -DNO_XMALLOC -Dset_relocation_prefix=libiconv_set_relocation_prefix -Drelocate=libiconv_relocate -DHAVE_CONfig_H -c ./iconv.c
libtool: compile: gcc -I. -I. -I../include -I./../include -I.. -I./.. -g -O2 -fvisibility=hidden -DLIBDIR=\"/usr/local/lib\" -DBUILDING_LIBICONV -DBUILDING_DLL -DENABLE_RELOCATABLE=1 -DIN_LIBRARY -DINSTALLDIR=\"/usr/local/lib\" -DNO_XMALLOC -Dset_relocation_prefix=libiconv_set_relocation_prefix -Drelocate=libiconv_relocate -DHAVE_CONfig_H -c ./iconv.c -fPIC -DPIC -o .libs/iconv.o
In file included from ./iconv.c:112:
./encodings.def:1024: error: 'iso2022_kr_mbtowc' undeclared here (not in a function)
./encodings.def:1024: error: 'iso2022_kr_wctomb' undeclared here (not in a function)
./encodings.def:1024: error: 'iso2022_kr_reset' undeclared here (not in a function)
make[1]: *** [iconv.lo] Error 1
make[1]: Leaving directory `/root/example/libiconv-1.14/lib'
b.接着修改encodings.def文件,屏蔽文件中的以下内容
DEFENCODING(( "ISO-2022-KR",/* IANA,RFC 1557 */
"csISO2022KR",/* IANA */
/*"ISO2022KR",JDK 1.1 */
),
iso2022_kr,Arial; line-height:26px"> { iso2022_kr_mbtowc,NULL },{ iso2022_kr_wctomb,iso2022_kr_reset })
在终端中继续执行make,看看又有什么错误会出现。
cd lib && make all
make[1]: Entering directory `/root/example/libiconv-1.14/lib'
In file included from ./iconv.c:154:
lib/aliases.gperf:356: error: 'ei_iso2022_kr' undeclared here (not in a function)
c.最后,修改aliases.h文件(这步比较关键,如果不细心的话,可能在实际应用的时候会找不到你所需要的编码,造成不必要的麻烦)。在该文件中,找到有ei_iso2022_kr的地方,作如下修改
#line 355 "lib/aliases.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str362,ei_iso2022_kr},Arial; line-height:26px"> #else
{-1},Arial; line-height:26px"> #line 356 "lib/aliases.gperf"
{(int)(long)&((struct stringpool_t *)0)->stringpool_str334,Arial; line-height:26px"> 再一次make的时候,可以发现没有其它任何的错误
4、经过裁减后,库可以成功编译出来了,但整体的编译可能还是存在一些问题,原因是你删除了相关的编码方式,而那些程序恰好使用到了它。以下是我遇到的一个问题的解决方法
问题:make进入libiconv-1.14/src目录不能编译。
解决方法:不编译此目录。由于这里面的Makefile是根据Makefile.in文件生成的,所以直接修改Makefile.in
屏蔽以下两句
all : iconv_no_i18n$(EXEEXT) iconv.@OBJEXT@ $(OBJECTS_RES_@woe32@)
test `ls -ld . | sed -e 's/^d\(.........\).*/\1/'` = rwxrwxrwx || chmod 777 .
新增
all:
如果只是删减一个的话,看不出有什么太大的变化,可以删除多个试试!

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念