Cant use shared libraries in Qt project

南笙酒味 提交于 2019-12-30 22:45:07

问题


I created a C++ library project in Qt creator. After building the project I have the libMylib.so, .so.1, .so.1.0, .so.1.0.0, Makefile and mylib.o files. I added the library headers to my other project and added the path to my .pro file like this:

LIBS += "/home/peter/Workspace/build-Libtester-Desktop-Release/libMyLib.so"

When building the application I don't get no such file error, but when running it I get this:

/home/peter/Workspace/build-Libtester-Desktop-Debug/Libtester: error while loading shared libraries: libMyLib.so.1: cannot open shared object file: No such file or directory

which I can't understand, because it's right there next to the .so which it seem to find, because when the path is wrong I get a no such file or directory error when trying to build the project. Could someone explain what I'm missing here?

Thanks for your time.


回答1:


You are adding the library incorrectly. You are doing:

LIBS += "/home/peter/Workspace/build-Libtester-Desktop-Release/libMyLib.so"

instead of:

LIBS += -L"/home/peter/Workspace/build-Libtester-Desktop-Release" -lMyLib

The first version works on windows, but not linux.

Basically, you create a library, which will be named "libMyLib.so", and then you specify the path to its folder, prepended by "-L" and then do "-lMyLib" at the end, note that it's not "-llibMyLib", but just "-lMyLib", despite the fact that the .so name is "libMyLib".

Look here: https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application for more info.




回答2:


Fortunately, your problem has nothing to do with both Qt and Qt Creator. The error simply boils down to how shared libraries are searched by LD for dynamic linking on Unix OS family.

Today, I've answered similar question, have a look, please. This question was asked in regard to Mac OS X. However, Linux and Mac OS X are the same in the context of your problem. I've provided additional explanation for Linux at the bottom, so pay attention to it. "it's right there next to the .so" - you seem to have Windows background if you make this assumption, but it is wrong for Unix OS family altogether (as stated in the answer too). If you have further questions, just ask.



来源:https://stackoverflow.com/questions/16071409/cant-use-shared-libraries-in-qt-project

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