Bug in GCC or Linking shared library

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Everyone,

I have two shared library linked to executable and both library has a
function called 'foo()' but both function signature differs. When
foo() is called in executable the call to foo() is executed from
unexpected shared library.

Call to function foo()  is depended on order of shared library linked
while building executable. For example if executable built using
libx.so first in order then foo() is called out of libx.so library and
if liby.so is placed first then foo() is called out of liby.so, even
though foo() in liby.so has a completely different signature but still
foo() is called out of liby.so if liby.so linked before libx.so.

I have tried with few other gcc flags but all ended with same problem.

It will be great help if somebody help me to fix this issue.

Thanks for the help.


build shared library:

gcc -m32 -g -c libx.c
gcc -shared -o libx.so libx.o

gcc -m32 -g -c liby.c
gcc -shared -o liby.so liby.o

build executable:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.

gcc -m32 -g main.c -o a.out libx.so liby.so     (if i build in this
order then get_char() is called out of libx.so)

./a.out
LibX is called ....
LibX is called ....

gcc -m32 -g main.c -o a.out  liby.so libx.so     (and if i build in
this order then get_char() is called out of liby.so)

./a.out
LibY is called ....
LibY is called ....

Below code tested on following GCC versions
4.1.2-48 - Centos5/RedHat5
4.2.1 - Mac 10.7
4.4.3 - Ubuntu 10.4
4.6 - Ubuntu 12

libx.h:

int get_int(int val);
char get_char(char ch);

libx.c:

#include <stdio.h>
#include "libx.h"

int get_int(int val) {
    printf("LibX is called ... \n");
    return val;
}

char get_char(char ch) {
   printf("LibX is called .... \n");
   return ch;
}

liby.h:

int get_int(int val);

typedef struct {
  double db;
  int i;
  char c;
} Y;

int get_char(Y st_Y, double val);

liby.c:

#include <stdio.h>
#include "liby.h"

int get_int(int val) {
    printf("LibY is called ... \n");
    return val;
}

int get_char(Y st_y, double val) {
    printf("LibY is called .... \n");
    return val;
}


main.c:

int main(int argc, char **argv)  {

     get_num(0);
     get_char('c');

     return 0;

}


Vijay
;)


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux