This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: symbol collision


Hello,

I have two functions with same name but with different proto-types
defined in two different libraries. Somehow linker seems to be picking
silently(no multiple definition errors) unmatched version of the
function while linking.

To illustrate the problem, I created an example code-snippet with the
following arrangement.

 $] cat 1.h
#ifndef 1_H_
#define 1_H_

int zlib_compress(void);
#endif // 1_H_

$ cat 1.c

#include "1.h"
#include "2.h"

int zlib_compress(void)
{
  int a;
  crc32(&a);
}

]$ cat 2.h

#ifndef 2_H_
#define 2_H_

extern int crc32(int *a);
#endif // _2_H_

]$ cat main.c
#include <stdio.h>
#include "1.h"

int crc32(int a, int b)
{
  printf("crc32 in main.c\n");
}

int main()
{
  printf("Calling Zlib compress\n");
  zlib_compress();
}

The executable "main" was created the following way.

[linker]$ gcc -I./ 1.c -c
[linker]$ gcc -I./ 2.c -c
[linker]$ ar rcs lib12.a 1.o 2.o
[linker]$ gcc main.o -L./ -l12 -o main
[linker]$./main
Calling Zlib compress
crc32 in main.c

Here I was expecting linker to throw multiple-definition error perhaps
it choose "crc32(int, int)" defined in main.o to link in the final
executable.  Any reason why linker is choosing "crc32" from main.o
rather than from lib12.a itself ?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]