Labels

25/03/2011

Linux library link configuration (libquazip.so)

In Linux system, understanding of how system or application library works is very important. Many applications you download are source files which need to be compiled. for example: qmake, make, make install.
After "make" the source file (typically lib), the lib files will be created, such as:

ln -s libquazip.so.1.0.0 libquazip.so
ln -s libquazip.so.1.0.0 libquazip.so.1
ln -s libquazip.so.1.0.0 libquazip.so.1.0

but these lib file are not installed in Linux system.
After "make install", all lib files has been installed in Linux system.

ln -f -s "libquazip.so.1.0.0" "/usr/local/lib/libquazip.so"
ln -f -s "libquazip.so.1.0.0" "/usr/local/lib/libquazip.so.1"
ln -f -s "libquazip.so.1.0.0" "/usr/local/lib/libquazip.so.1.0"


However, even you "make install" lib files, you may still get error message when you run Qt application. for example, I "make install" QuaZIP package, and successfully compile a test.pro which come with this package, but i still got error message when i run the program:

Starting /home/czhang/quazip-0.3/test/zip-build-desktop/zip...
/home/czhang/quazip-0.3/test/zip-build-desktop/zip: error while loading shared libraries: libquazip.so.1: cannot open shared object file: No such file or directory
/home/czhang/quazip-0.3/test/zip-build-desktop/zip exited with code 127


Why?!! I have been struggling on this problem for long time.

Here is what i understand and solve the problem.

First of all, take a look at "ldconfig" - configure dynamic linker run-time bindings
$ man ldconfig
All applications, if they need lib to run, will check from here first /etc/ld.so.conf
take a look ld.so.conf:
$ cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf


It re-direct to /etc/ld.so.conf.d  and all .conf file will be checked through.
Let's take a look at ld.so.conf.d folder
$ ls /etc/ld.so.conf.d

GL.conf            libasound2.conf   x86_64-linux-gnu.conf
lib32asound2.conf  libc.conf        Qtlib.conf


in here, we can see a Qtlib.conf file. If you take a look at it, you will see all Qt lib is setting in
/home/czhang/qtsdk-2010.04/lib

When I "make install" QuaZip package, I got message like:

ln -f -s "libquazip.so.1.0.0" "/usr/local/lib/libquazip.so"
ln -f -s "libquazip.so.1.0.0" "/usr/local/lib/libquazip.so.1"
ln -f -s "libquazip.so.1.0.0" "/usr/local/lib/libquazip.so.1.0"


This means all QuaZip libs are stored in /usr/local/lib/

So, I create a new .conf file which call qauzip.conf
$ gedit qauzip.conf
add "/usr/local/lib" in it.

From now on, when Qt tries to use QuaZip lib, it can be found.

Re-compile my "test.pro", and run it, works! :)

Happy end.

EDIT:
or, just simply
$ sudo cp -a /home/czhang/quazip-0.3/quazip libquazip* /usr/lib64
=================================================
EDIT:

when i compile my own program which including QuaZip, i got error messages:

error: undefined reference to `QuaZip::QuaZip(QString const&)'
error: undefined reference to `QuaZip::open(QuaZip::Mode, zlib_filefunc_def_s*)'
...

Then I took a look .pro file,

INCLUDEPATH += /home/czhang/quazip-0.3/quazip
LIBS += -L -lquazip

Actually, I don't really understand the -L. After doing some studies, i learned:

INCLUDEPATH:
This variable specifies the #include (normally header file) directories which should be searched when compiling the project.

LIBS:
This variable contains a list of libraries to be linked into the project. You can use the Unix -l (library) and -L (library path) flags and qmake will do the correct thing with these libraries on Windows (namely this means passing the full path of the library to the linker). The only limitation to this is the library must exist, for qmake to find which directory a -l lib lives in.

Therefore, my .pro file should be like:


INCLUDEPATH += /home/czhang/quazip-0.3/quazip
LIBS += -L/home/czhang/quazip-0.3/quazip -lquazip
Or:
INCLUDEPATH += /home/czhang/quazip-0.3/quazip
LIBS += -lquazip


$ rsync -av czhang@10.0.1.202://build/czhang/qt-everywhere-op
ensource-src-4.7.0/projects/quazip-0.3/quazip/libquazip.so* /usr/lib

1 comment:

  1. Anonymous2/8/12 07:58

    Thanks a lot! This was very useful information. I've been struggling with getting quazip to work in my Qt app as well. Great post.

    ReplyDelete