假设我有以下 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 个不同的库来构建它:l1
、l2
、l3
——假设它们每个都导出它的拥有 f
和 g
函数——因此:
f_1
和g_1
将从l1
分别解析为f
和g
>;f_2
和g_2
将从l2
分别解析为f
和g
>;f_3
和g_3
将从l3
分别解析为f
和g
>.
gcc 和 ld 是否可行:
- 如果
l1
、l2
、l3
是共享库 (.so),这可能吗?< - 如果
l1
、l2
、l3
是存档文件 (.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.
将它应用于您的库 l1
、l2
、l3
。它应该适用于 .a 和 .so。
关于c - 在 ld 中明确指定符号的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37796518/