1

I want to link two shared libraries that both two libraries have same symbol name but different symbol type.

For example, there are liba.so and libb.so libraries that consist of two files a.c/b.c and b_wrapper.c/a_wrapper.c

a.c

__thread int var;

a_wrapper.cpp

extern __thread int var;

liba.so

$ gcc -fPIC -c a.c -o a.o 
$ g++ -fPIC -c a_wrapper.cpp -o a_wrapper.o -std=c++11
$ g++ -fPIC -shared  -o liba.so a.o a_wrapper.o -std=c++11

b.c

int var;

b_wrapper.c

extern int var;

libb.so

$ gcc -fPIC -c b.c -o b.o 
$ g++ -fPIC -c b_wrapper.cpp -o b_wrapper.o -std=c++11
$ g++ -fPIC -shared  -o libb.so b.o b_wrapper.o -std=c++11

then link liba.so and libb.so to top.c file, I get some error message

$ g++ top.cpp -ldl -o exe ./liba.so ./libb.so  -std=c++11

/usr/bin/ld: var: TLS definition in ./liba.so section .tdata mismatches non-
TLS definition in ./libb.so section .bss
./libb.so: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

I think the problem is because var in a.c is defined in thread local storage bss(tbss), but var in b.c is defined in normal bss.


I have read this post Linking two shared libraries with some of the same symbols and try -Bsymbolic, --version-script and -fvisibility=hidden, but all these methods can not achieve my goal.

-Bsymbolic : I used it when creating liba.so and libb.so, but when I linked liba.so and libb.so I still got same error message described as above

--version-script and -fvisibility=hidden : I can't let var variable become local to one file(a.c) since other files(a_wrapper.cpp) will extern and use that variable

Is there any way that I can link two shared libraries that has some same symbol name but different symbol type?

Eva
  • 11
  • 3

0 Answers0