如何绕过“多个定义的符号”与 gcc 链接

发布于 2024-09-10 08:33:05 字数 132 浏览 2 评论 0原文

我使用的是具有 gcc 2.95.3 的旧系统,我必须链接两个对象,尽管它们彼此无关,但它们每个都有相似的命名方法。我无法重命名它们中的任何一个,但我希望有一种方法来构建它们,以免链接器抱怨。它所抱怨的方法都是由对象内的类在内部调用的。我能做些什么?

I am using an older system that has gcc 2.95.3, I have to link in two objects that although they have nothing to do with each other, they each have similarly named methods. I can't rename either of them, but I would hope there is a way to build them as to not have the linker complain. The methods it is complaining about are each internally called by classes within the object. What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2024-09-17 08:33:05

如果您有完整的 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
$ 

因此,使用 objcopy 来更改一个(或两个,如果您愿意的话!)对象中 clash 的可见性:

$ objcopy --localize-symbol=clash bar.o bar2.o
$ 

现在您可以成功地与修改后的对象链接 - 并且程序的行为应如此:

$ gcc -o test main.o foo.o bar2.o
$ ./test
foo: Hello
bar: world
$ 

If you have a complete GNU toolchain, you should be able to work around your problem using objcopy, like this (if I've understood your problem correctly):

Here are two very similar objects, "foo" and "bar", both of which export a symbol called clash - which is used internally, but doesn't really need to be exported at all:

$ 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); }
$ 

And here's the main code, which wants to use both:

$ cat main.c
extern void foo(char *s);
extern void bar(char *s);

int main(void)
{
    foo("Hello");
    bar("world");
    return 0;
}
$ 

Linking them together doesn't work:

$ 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 to change the visibility of clash in one (or both, if you like!) of the objects:

$ objcopy --localize-symbol=clash bar.o bar2.o
$ 

Now you can link successfully with the modified object - and the program behaves as it should:

$ gcc -o test main.o foo.o bar2.o
$ ./test
foo: Hello
bar: world
$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文