C 在Unix上执行共享库

C 在Unix上执行共享库,c,unix,shared-libraries,C,Unix,Shared Libraries,某些Unix共享库在从命令行调用时提供输出,就像它们是可执行文件一样。例如: $ /lib/libc.so.6 GNU C Library stable release version 2.13, by Roland McGrath et al. Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO w

某些Unix共享库在从命令行调用时提供输出,就像它们是可执行文件一样。例如:

$ /lib/libc.so.6 
GNU C Library stable release version 2.13, by Roland McGrath et al.
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.5.2.
Compiled on a Linux 2.6.37 system on 2011-01-18.
[...]
在我自己用C编写的共享库中,如何提供此输出?我现在已经执行了一个我刚刚创建的库,我得到了一个段错误

注意:我以前在unix.stackechange.com上问过这个问题
下面的main定义负责打印您看到的输出。它在glibc源代码树的csu/version.c中定义。我希望这有帮助

#ifdef HAVE_ELF /* This function is the entry point for the shared object. Running the library as a program will get here. */ extern void __libc_main (void) __attribute__ ((noreturn)); void __libc_main (void) { __libc_print_version (); _exit (0); } #endif #如果你有小精灵 /*此函数是共享对象的入口点。 将库作为程序运行将到达此处*/ 外部无效uuu libc_main(无效)uuuuu属性uuuuu((noreturn)); 无效的 __libc_main(无效) { __libc_打印版本(); _出口(0); } #恩迪夫
另见@BrianL。谢谢,很有趣!