Linux 如何链接 glibc 的 iconv 实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4709178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I link glibc's implementation of iconv?
提问by x-x
The GNU C library provides an implementation of iconv - how do I use it?
GNU C 库提供了 iconv 的实现 - 我该如何使用它?
Simple program:
简单程序:
#include <iconv.h>
int main( int argc, char **argv ) {
iconv_t cd = iconv_open( "UTF-8", "ISO-8859-1" );
iconv_close( cd );
return 0;
}
Compile and link:
编译并链接:
$ 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
List the symbols to show they exist!
列出符号以表明它们存在!
$ nm -D /lib/libc-2.12.1.so | grep iconv
00017920 T iconv
00017ae0 T iconv_close
00017720 T iconv_open
If I install the GNU libiconv library to /usr/local and link with -liconv it works. How do I link with the glibc implementation of iconv?
如果我将 GNU libiconv 库安装到 /usr/local 并与 -liconv 链接,它就可以工作。如何与 iconv 的 glibc 实现链接?
EDIT: More information as requested from the comments:
编辑:根据评论的要求提供更多信息:
List all iconv.h files in /usr (1 match)
列出 /usr 中的所有 iconv.h 文件(1 个匹配项)
$ find /usr/ | grep "iconv\.h"
/usr/include/iconv.h
Reinstall libc6-dev to ensure the correct header is installed.
重新安装 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) ...
Compile and link again with suggested preprocessor option:
使用建议的预处理器选项再次编译和链接:
$ 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
Output from gcc -H:
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
pastbin copy of /usr/include/iconv.h
/usr/include/iconv.h 的过去 bin 副本
Fixed: Reboot fixed the issue. I suspect a cached copy of libiconv was causing the conflicts, even though it was deleted from disk.
修复:重新启动修复了问题。我怀疑 libiconv 的缓存副本导致了冲突,即使它已从磁盘中删除。
采纳答案by thkala
Your program seems fine and compiles fine on my system (Mandriva Linux 2010.1).
你的程序看起来很好,在我的系统上编译得很好(Mandriva Linux 2010.1)。
I find the libiconv_*
references in your compile log worrisome, though. Are you sure that the iconv.h
version that gets included comes from glibc and not from a separate libiconv implementation, such as GNU libiconv? It sounds as if it adds a lib
prefix to all iconv functions to avoid symbol collisions with the iconv implementation of the C library that came with the system.
不过,我发现libiconv_*
编译日志中的引用令人担忧。您确定包含的iconv.h
版本来自 glibc 而不是来自单独的 libiconv 实现,例如 GNU libiconv?听起来好像它lib
为所有 iconv 函数添加了一个前缀,以避免与系统附带的 C 库的 iconv 实现发生符号冲突。
Having to explicitly link to libiconv points to a separate iconv implementation too - glibc does not need it.
必须显式链接到 libiconv 也指向单独的 iconv 实现 - glibc 不需要它。
EDIT:
编辑:
For the record, I just verified that using the iconv.h
header file from libiconv without explicitly linking against it will produce exactly the result that you are seeing - it renames all iconv functions by adding a lib
prefix to their names.
作为记录,我刚刚验证了使用iconv.h
libiconv 中的头文件而不显式链接它会产生与您看到的完全相同的结果 - 它通过lib
在名称中添加前缀来重命名所有 iconv 函数。