无法在Qt项目中使用共享库 [英] Cant use shared libraries in Qt project
问题描述
我在Qt Creator中创建了一个C ++库项目.构建项目后,我有了libMylib.so,.so.1,.so.1.0,.so.1.0.0,Makefile和mylib.o文件.我将库标头添加到其他项目中,并将路径添加到我的.pro文件中,如下所示:
LIBS += "/home/peter/Workspace/build-Libtester-Desktop-Release/libMyLib.so"
在构建应用程序时,我没有收到此类文件错误,但是在运行它时,却显示了以下信息:
/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
我无法理解的
,因为它就在它似乎找到的.so旁边,因为当路径错误时,在尝试构建项目时出现这样的文件或目录错误. 有人可以解释一下我在这里想念的东西吗?
感谢您的时间.
您错误地添加了库.您正在做
LIBS += "/home/peter/Workspace/build-Libtester-Desktop-Release/libMyLib.so"
代替:
LIBS += -L"/home/peter/Workspace/build-Libtester-Desktop-Release" -lMyLib
第一个版本适用于Windows,但不适用于Linux.
基本上,您将创建一个名为"libMyLib.so"的库,然后指定其文件夹的路径,以"-L"开头,然后在末尾执行"-lMyLib",请注意,它是尽管".so"名称为"libMyLib",但不是"-llibMyLib",而只是"-lMyLib".
查看此处: https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_in_an_application . >
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.
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.
这篇关于无法在Qt项目中使用共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!