如果您有完整的 GNU 工具链,您应该能够使用以下方法解决您的问题objcopy
,像这样(如果我正确理解你的问题):
这是两个非常相似的对象,“foo”和“bar”,它们都导出一个名为clash
- 这是内部使用的,但根本不需要导出:
$ cat foo.c
#include <stdio.h>
void clash(char *s) { printf("foo: %s\n", s); }
void foo(char *s) { clash(s); }
$
and
$ cat bar.c
#include <stdio.h>
void clash(char *s) { printf("bar: %s\n", s); }
void bar(char *s) { clash(s); }
$
这是想要同时使用两者的主要代码:
$ cat main.c
extern void foo(char *s);
extern void bar(char *s);
int main(void)
{
foo("Hello");
bar("world");
return 0;
}
$
将它们链接在一起不起作用:
$ gcc -Wall -c foo.c
$ gcc -Wall -c bar.c
$ gcc -Wall -c main.c
$ gcc -o test main.o foo.o bar.o
bar.o: In function `clash':
bar.c:(.text+0x0): multiple definition of `clash'
foo.o:foo.c:(.text+0x0): first defined here
collect2: ld returned 1 exit status
$
So, use objcopy
改变可见性clash
在一个(或两个,如果你愿意的话!)对象中:
$ objcopy --localize-symbol=clash bar.o bar2.o
$
现在您可以成功链接到修改后的对象 - 并且程序将按其应有的方式运行:
$ gcc -o test main.o foo.o bar2.o
$ ./test
foo: Hello
bar: world
$