Cant use shared libraries in Qt project
Asked Answered
M

2

5

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.

Michelsen answered 17/4, 2013 at 22:33 Comment(1)
Add DEPENDS += "/home/peter/Workspace/build-Libtester-Desktop-Release/" does it work? It will make Qt creator search in that location for runtime lib.Siloa
C
3

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.

Conform answered 10/10, 2016 at 22:17 Comment(0)
G
4

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.

Gumbo answered 17/4, 2013 at 23:31 Comment(1)
It works from command line, but how do we specify the DYLD_LIBRARY_PATH in Qt?Disorganization
C
3

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.

Conform answered 10/10, 2016 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.