Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant use shared libraries in Qt project

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.

like image 484
Peter Avatar asked Apr 17 '13 22:04

Peter


2 Answers

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.

like image 52
Alexander Shukaev Avatar answered Sep 28 '22 00:09

Alexander Shukaev


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.

like image 25
Pavel Avatar answered Sep 28 '22 02:09

Pavel