在Unix上执行共享库

时间:2011-02-10 21:38:29

标签: 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 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上问过这个问题 https://unix.stackexchange.com/questions/7066/executing-a-shared-library

1 个答案:

答案 0 :(得分:11)

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