I’m creating a library that depends on other libraries (libpng
, libX11
, etc.). I would like to know if it is possible (with some flags, for example) for the user binary not to directly link to the third party libraries but rather indirectly via my lib.
Here is an example:
libb.c
(as the third party lib)
int get21()
{ return 21; }
liba.c
(as my lib)
int get21();
int get42()
{ return get21() * 2; }
main.c
(as the user code)
int get21();
int get42();
int main()
{
printf("42 = %d
21 = %d
", get42(), get21());
return 0;
}
Compilation
$ gcc -fPIC -shared libb.c -o libb.so
$ gcc -fPIC -shared liba.c -L. -lb -Wl,-rpath=. -o liba.so
$ gcc main.c -L. -la -Wl,-rpath=.
/usr/bin/ld: /tmp/ccVm8exQ.o: undefined reference to symbol 'get21'
./libb.so: error adding symbols: DSO missing from command line
Normally, I would need to link the main with -lb
too. But I don’t want to final user to have to link against all libraries, as it is cumbersome and might change in the future. Is there a possibility of avoiding that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…