This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: static link problems with alias
Hi Carmelo,
I've encountered a problem doing a static link due to duplicated symbols
error.
You appear to misunderstand how aliases and libraries work...
I have the following dummy functions:
- int foo();
- foo_alias() as weak alias of foo()
- int bar(); (thats call foo_alias())
all archived into libfoo_alias.a
Note that in your libfoo_alias.a you have a *strong* definition of a
symbol called "foo" and a *weak* definition of a symbol called "foo_alias".
and the following ones:
int foo();
int other();
archied into libfoo.a
Note that in libfoo.a you have a *second* strong definition of a symbol
called "foo".
and the main.c calling bar() and other().
When I compile the main with the following command:
gcc -static -L. main.c -lfoo_alias -lfoo I get the following error:
./libfoo.a(foo.o)(.text+0x0): In function `foo':
: multiple definition of `foo'
./libfoo_alias.a(foo_alias.o)(.text+0x0): first defined here
Which is quite correct. It goes like this:
1) main calls bar. bar is defined in libfoo_alias:bar.o
2) bar calls foo_alias. foo_alias is weakly defined in
libfoo_alias:foo.o, but there are no strong definitions provided
elsewhere, so the weak definition is used. Hence libfoo_alias:foo.o is
included in the link which brings with it a strong definition of the
"foo" symbol.
3) main calls other. other is defined in libfoo.a:foo.o.
libfoo.a:foo.o also defines "foo", hence there is now a duplicate
definition.
What you probably wanted was the following:
1) Edit foo.c and add these lines to the end:
int foo_alias () {
return 2;
}
This provides a strong definition of the foo_alias symbol which can
override the weak definition in foo_alias.c.
2) Change the gcc link command line to:
gcc -static -L. main.c -lfoo -lfoo_alias
ie moving the linking of libfoo.a to before libfoo_alias.a so that
the strong definition of foo_alias is available when the reference to it
is encountered in libfoo_alias:bar.o.
Cheers
Nick