Linker does not emit multiple detection errors when the same character coexists in an object file and a static library

Given a compiled function with a signature void some_func()in the static library and another with the same signature void some_func()in the object file, you expect that when linking them together, a "multiple definition" error should occur.

But this is not so. As far as I noticed linkers (tested with GCC and MSVC software suites), select the implementation located in the object file without any errors or warnings.

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 help create (create the 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

"Troubling func" , .

- , ?

+4
1

GNU ld, GCC, , , .

  • , , .

  • "" .

  • . , , , , . , . , , .

, :

"some_func in library\n"

"some_func in somelib.o"

, , :

1

troublingsource.o somelib.o .

: 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

:

  • main.o .
  • main main.o
  • some_func , main.o
  • troublingsource.o
  • some_func, , , troublingsource.o.
  • . libsomelib.a .

2

someblib.o troublingsource.o .

: 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

:

  • main.o .
  • main main.o
  • some_func , main.o
  • someblib.o
  • some_func, , , somelib.o.
  • . libtroublingsource.a .

3

someblib.o troublingsource.o .

: 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

:

  • main.o .
  • main main.o
  • some_func , main.o
  • libsomeblib.a , some_func
  • some_func somelib.o of libsomelib.a
  • somelib.o libsomelib.a .
  • . libtroublingsource.a .

4

someblib.o troublingsource.o .

: 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

:

  • main.o .
  • main main.o
  • some_func , main.o
  • someblib.o
  • some_func, , , somelib.o
  • troublingsource.o
  • some_func, somelib.o, troublingbsource.o. .

5

someblib.o .

: 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

:

  • libsomelib.a , . , undefined, libsomelib.a ,
  • main.o .
  • main main.o
  • some_func , main.o
  • . some_func , undefined. .
+5

All Articles