问题
Given a compiled function with signature void some_func() in a static library, and another one with the same void some_func() signature in a object file one expects that when you link them together a "multiple definition" error should have occured.
But this is not the case. As far as i've observed the linkers (tested with GCC and MSVC toolchains) choose the implementation residing in the object file without emitting any error or warning.
Given the following POC:
somelib.h
#ifndef _SOMELIB_H_
#define _SOMELIB_H_
void some_func();
#endif /* _SOMELIB_H_ */
somelib.c
#include "somelib.h"
#include <stdio.h>
void some_func()
{
printf("some_func in library\n");
}
troublingheader.h
#ifndef _TROUBLING_HEADER_H_
#define _TROUBLING_HEADER_H_
void some_func();
#endif /* _TROUBLING_HEADER_H_ */
troublingsource.c
#include "troublingheader.h"
#include <stdio.h>
void some_func()
{
printf("Troubling func\n");
}
main.c
#include <stdio.h>
#include "somelib.h"
int main()
{
some_func();
}
And a simple Makefile to assist building (create tmp folder first):
tmp/wat.exe: tmp/libsomelib.a tmp/main.c.o tmp/troublingsource.c.o
gcc -static -static-libgcc -Ltmp -otmp/wat.exe tmp/main.c.o tmp/troublingsource.c.o -lsomelib
tmp/main.c.o:
gcc -Wall -Wextra -c -g -O0 src/main.c -o tmp/main.c.o
tmp/troublingsource.c.o:
gcc -Wall -Wextra -c -g -O0 src/troublingsource.c -o tmp/troublingsource.c.o
tmp/somelib.o:
gcc -Wall -Wextra -c -g -O0 src/somelib.c -o tmp/somelib.c.o
tmp/libsomelib.a: tmp/somelib.o
ar rcs tmp/libsomelib.a tmp/somelib.c.o
When running the final executable, the contents "Troubling func" are shown as above described.
Can someone explain me why this happens?
回答1:
In the case of the GNU linker ld, invoked by GCC, what you have observed is explained by the following points.
An object file in the linkage sequence is added to the linkage unconditionally, whether it contains symbols that the program needs or not.
A static library is an archive of object files together with a "table of contents" that the linker can inspect.
By default, an object file within a static library in the linkage sequence is not added to the linkage unconditionally. The linker searches a static library only to find definitions of symbols that it has observed to be referenced, but not defined, by object files already added to the linkage. An object file is extracted from the library and added to the linkage only if it provides definitions of such symbols. It is added only if the linker already needs some of the definitions that it provides.
For the avoidance of confusion, we'll change the output message:
"some_func in library\n"
to:
"some_func in somelib.o"
Then a demonstration of these points that explains what you've seen:
Case 1
Link troublingsource.o itself and somelib.o from a static library.
Linkage sequence: main.o, troublingsource.o, libsomelib.a
gcc -I. -c -o troublingsource.o troublingsource.c
gcc -I. -c -o somelib.o somelib.c
gcc -I. -c -o main.o main.c
ar rcs libsomelib.a somelib.o
gcc -o test main.o troublingsource.o -L. -lsomelib
./test
Troubling func
Here:
main.owas added unconditionally to the linkage.- Symbol
mainwas found defined inmain.o - Symbol
some_funcwas found referenced but not defined inmain.o troublingsource.owas added unconditionally to the linkage- Symbol
some_func, previously referenced but not defined, was found defined introublingsource.o. - No unresolved references remained.
libsomelib.awas not even searched.
Case 2
Link someblib.o itself and troublingsource.o from a static library.
Linkage sequence: main.o, somelib.o, libtroublingsource.a
gcc -I. -c -o somelib.o somelib.c
gcc -I. -c -o troublingsource.o troublingsource.c
gcc -I. -c -o main.o main.c
ar rcs libtroublingsource.a troublingsource.o
gcc -o test main.o somelib.o -L. -ltroublingsource
./test
some_func in somelib.o
Here:
main.owas added unconditionally to the linkage.- Symbol
mainwas found defined inmain.o - Symbol
some_funcwas found referenced but not defined inmain.o someblib.owas added unconditionally to the linkage- Symbol
some_func, previously referenced but not defined, was found defined insomelib.o. - No unresolved references remained.
libtroublingsource.awas not even searched.
Case 3
Link someblib.o and troublingsource.o from separate static libraries.
Linkage sequence: main.o, libsomelib.a, libtroublingsource.a
gcc -I. -c -o somelib.o somelib.c
gcc -I. -c -o troublingsource.o troublingsource.c
gcc -I. -c -o main.o main.c
ar rcs libsomelib.a somelib.o
ar rcs libtroublingsource.a troublingsource.o
gcc -o test main.o -L. -lsomelib -ltroublingsource
./test
some_func in somelib.o
Here:
main.owas added unconditionally to the linkage.- Symbol
mainwas found defined inmain.o - Symbol
some_funcwas found referenced but not defined inmain.o libsomeblib.awas searched for an object file providing a definition ofsome_func- A definition of
some_funcwas found in membersomelib.ooflibsomelib.a somelib.owas extracted fromlibsomelib.aand added to the linkage.- No unresolved references remained.
libtroublingsource.awas not even searched.
Case 4
Link someblib.o itself and troublingsource.o itself.
Linkage sequence: main.o, somelib.o, troublingsource.o
gcc -I. -c -o somelib.o somelib.c
gcc -I. -c -o troublingsource.o troublingsource.c
gcc -I. -c -o main.o main.c
gcc -o test main.o somelib.o troublingsource.o
troublingsource.o: In function `some_func':
troublingsource.c:(.text+0x0): multiple definition of `some_func'
somelib.o:somelib.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
Here:
main.owas added unconditionally to the linkage.- Symbol
mainwas found defined inmain.o - Symbol
some_funcwas found referenced but not defined inmain.o someblib.owas added unconditionally to the linkage- Symbol
some_func, previously referenced but not defined, was found defined insomelib.o troublingsource.owas added unconditionally to the linkage- Symbol
some_func, allready defined insomelib.o, was found defined again introublingbsource.o. Error.
Case 5
Link someblib.o from a static library.
Linkage sequence: libsomelib.a main.o
gcc -I. -c -o somelib.o somelib.c
gcc -I. -c -o main.o main.c
ar rcs libsomelib.a somelib.o
gcc -o test -L. -lsomelib main.o
main.o: In function `main':
main.c:(.text+0xa): undefined reference to `some_func'
collect2: error: ld returned 1 exit status
Here:
libsomelib.awas encountered before any object files were added to the linkage. At that point no symbols were referenced but undefined, solibsomelib.awas not even searched,main.owas added unconditionally to the linkage.- Symbol
mainwas found defined inmain.o - Symbol
some_funcwas found referenced but not defined inmain.o - There were no further object files or libraries to link.
some_funcwas finally an undefined reference. Error.
来源:https://stackoverflow.com/questions/37490157/linker-does-not-emit-multiple-definition-error-when-same-symbol-coexists-in-obje