How to resolve shared library linking during compi

2019-07-20 17:50发布

问题:

I have built libshared.so with is dependent on libshared_dependent.so

Now, I am compiling app.bin which is using libshared.so, Now at compile time gcc wants me to specify -lshared_dependent other wise it gives error that some symbols not found.

Is there any way by which, At compile time I don't need to specify -lshared_dependent, I want that just specifying -lshared works?

回答1:

You just need to link libshared.so to libshared_dependent.so when you are producing libshared.so.

gcc -shared -o libshared.so -lshared_dependant file1.o file2.o 

This way, when you link your app to libshared.so it'll also link to whatever libshared.so depends on



回答2:

You shouldn't need to have app.bin link with libshared_dependent.so.

That said, the linker will want to verify the symbols in libshared.so can be found so it will need to locate libshared_dependent.so in order to do that. If libshared_dependent.so is in a different directory then libshared.so, you will want to specify the path to libshared_dependent.so using the linker option -rpath-link.

Because -rpath-link is a linker option, you will need to tell gcc to pass it through to the linker:

gcc -Wl,-rpath-link,/directory-that-libshared_dependent-is-in