如何知道.so库文件中的函数以及如何将特定的二进制代码链接到代码中?

问题描述 投票:0回答:1

linux中有共享库。例如,libcap.so.2.24。

当我写一些类似的东西时,我知道当我编写c代码时

#include <stdio.h>

int main(){
  printf("hello world\n");
  return 0;
}

gcc编译将自动包含库中的printf函数并生成异议文件。

我的问题是

  1. 除了使用编译之外还有其他方法来获取printf的二进制代码吗?
  2. 当编译在对象文件中包含printf时,它是否包含整个库libcap.so.2.24或仅包含与printf函数相关的部分?我猜它只包含与printf函数相关的部分,如果是这样,编译怎么做呢?
  3. 我可以手动包含库中的函数而无需使用编译器吗?
c linux-kernel embedded-linux
1个回答
0
投票
1) The compiler only includes the header files, not the library itself
2) During linking the library is bound to the executable.
   How this happens depends if static or dynamic linking is used
3) It is possible to bind a library at runtime with the dlopen library function
   and then use dlsym to get a function pointer.
   The dlopen man page has a very nice example
© www.soinside.com 2019 - 2024. All rights reserved.