c - 在 Unix 上执行共享库

标签 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 & Linux SE 上问过这个问题 here .

最佳答案

下面的 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

关于c - 在 Unix 上执行共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377740/

相关文章:

java - 当使用 JSch 通过 Java 执行时,某些 Unix 命令会失败并返回 "... not found"

linux - 可能的共享库冲突导致 SQLite 数据库损坏

与 Terry Guo 的 gcc-arm-none-eabi 交叉编译

C程序在运行时崩溃

linux - 如何收集所有 README 文件并在文件夹中重命名它们?

linux - 需要回答 2 个 Unix MCQ 问题

c++ - C++ 结构中私有(private)和 protected 可用性

c - C 中的未知类型名称错误

c - 如何从 .o 文件中删除 undefined symbol

c - 当调用 dlclose 时共享库中的全局变量会发生什么?