如何链接glibc的iconv的实现?

GNU C库提供了iconv的实现 – 我如何使用它?

简单的程序:

#include <iconv.h> int main( int argc, char **argv ) { iconv_t cd = iconv_open( "UTF-8", "ISO-8859-1" ); iconv_close( cd ); return 0; } 

编译和链接

 $ gcc -Wall iconv.c -o iconv /tmp/ccKAfXNg.o: In function `main': iconv.c:(.text+0x19): undefined reference to `libiconv_open' iconv.c:(.text+0x29): undefined reference to `libiconv_close' collect2: ld returned 1 exit status 

列出符号以显示它们存在!

 $ nm -D /lib/libc-2.12.1.so | grep iconv 00017920 T iconv 00017ae0 T iconv_close 00017720 T iconv_open 

如果我将GNU libiconv库安装到/ usr / local并且与-liconv链接,它就可以工作。 如何链接iconv的glibc实现?

编辑:更多的信息请求从评论:

列出/ usr(1匹配)中的所有iconv.h文件

 $ find /usr/ | grep "iconv\.h" /usr/include/iconv.h 

重新安装libc6-dev以确保安装了正确的头文件。

 $ dpkg -S /usr/include/iconv.h libc6-dev: /usr/include/iconv.h $ apt-get install --reinstall libc6-dev Reading package lists... Done Building dependency tree Reading state information... Done 0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded. Need to get 0B/4,910kB of archives. After this operation, 0B of additional disk space will be used. (Reading database ... 143458 files and directories currently installed.) Preparing to replace libc6-dev 2.12.1-0ubuntu10.1 (using .../libc6-dev_2.12.1-0ubuntu10.1_i386.deb) ... Unpacking replacement libc6-dev ... Setting up libc6-dev (2.12.1-0ubuntu10.1) ... 

使用build议的预处理器选项编译并重新链接:

 $ gcc -Wall -DLIBICONV_PLUG iconv.c -o iconv /tmp/ccKAfXNg.o: In function `main': iconv.c:(.text+0x19): undefined reference to `libiconv_open' iconv.c:(.text+0x29): undefined reference to `libiconv_close' collect2: ld returned 1 exit status 

gcc -H输出:

 $ gcc -H iconv.c . /usr/include/iconv.h .. /usr/include/features.h ... /usr/include/bits/predefs.h ... /usr/include/sys/cdefs.h .... /usr/include/bits/wordsize.h ... /usr/include/gnu/stubs.h .... /usr/include/bits/wordsize.h .... /usr/include/gnu/stubs-32.h .. /usr/lib/gcc/i686-linux-gnu/4.4.5/include/stddef.h Multiple include guards may be useful for: /usr/include/bits/predefs.h /usr/include/gnu/stubs-32.h /usr/include/gnu/stubs.h /usr/lib/gcc/i686-linux-gnu/4.4.5/include/stddef.h 

/usr/include/iconv.h的pastbin副本

修复:重新启动解决了这个问题。 我怀疑libiconv的一个caching副本正在导致冲突,即使它是从磁盘上删除的。

Solutions Collecting From Web of "如何链接glibc的iconv的实现?"

你的程序看起来很好,并在我的系统上编译(Mandriva Linux 2010.1)。

尽管如此,我在你的编译日志中发现了libiconv_*引用。 你确定包含的iconv.h版本来自glibc而不是来自单独的libiconv实现,比如GNU libiconv? 听起来好像它为所有iconv函数添加了一个lib前缀,以避免与系统随附的C库的iconv实现的符号冲突。

不得不显式链接到libiconv指向一个单独的iconv实现 – glibc不需要它。

编辑:

为了记录,我刚刚证实,使用iconv.h头文件而不显式地链接它,将会产生与您所看到的结果完全相同的结果 – 通过为它们的名称添加一个lib前缀来重命名所有iconv函数。