How to resolve shared library linking during compilation( GCC )?

老子叫甜甜 提交于 2019-12-13 16:05:36

问题


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


来源:https://stackoverflow.com/questions/1926716/how-to-resolve-shared-library-linking-during-compilation-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!