c - 在 ld 中明确指定符号的位置

标签 c gcc ld

假设我有以下 C 代码:

extern int f_1();
extern int g_1();

extern int f_2();
extern int g_2();

extern int f_3();
extern int g_3();

int main(int argc, char **argv) {
    // Using f_1, f_2, f_3 and g_1, g_2, g_3 here:
    ...
}

我想通过链接 3 个不同的库来构建它:l1l2l3——假设它们每个都导出它的拥有 fg 函数——因此:

  • f_1g_1 将从 l1 分别解析为 fg >;
  • f_2g_2 将从 l2 分别解析为 fg >;
  • f_3g_3 将从 l3 分别解析为 fg >.

gcc 和 ld 是否可行:

  1. 如果 l1l2l3 是共享库 (.so),这可能吗?<
  2. 如果 l1l2l3 是存档文件 (.a),这是否可能?

最佳答案

objcopy--redefine-sym 选项是你的 friend :

--redefine-sym old=new

Change the name of a symbol old, to new. This can be useful when one is trying link two things together for which you have no source, and there are name collisions.

--redefine-syms=filename

Apply --redefine-sym to each symbol pair "old new" listed in the file filename. filename is simply a flat file, with one symbol pair per line. Line comments may be introduced by the hash character. This option may be given more than once.

将它应用于您的库 l1l2l3。它应该适用于 .a.so

关于c - 在 ld 中明确指定符号的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37796518/

相关文章:

c - 如何将 char 传递给函数?

c - ev_timer 回调被调用得早于应有的时间

c - 在C程序中使用cp命令获取要复制的文件列表

c - 在函数调用中用作数组索引的递增变量

c - 如何在 GNU LD 中实现自定义输出部分?

c - 使用自定义部分属性时附加符号名称

c - 如何检查哪个进程启动了 sys_open

c - 过程关联表和全局偏移表

c - Linux 上的 vsnwprintf 替代方案

android - 检查需要哪些库