PDA

View Full Version : Custom library problem



Dario
2nd February 2011, 17:20
Hello,

first of all, my aplogies for posting this again. I've researched the forum and I've found plenty of old posts similar to this one. The thing is, all of them were solved by modifying the .pro file, which has not worked for me (I think my pro file is ok). Instead of resurrecting a dead post I created a new one.

The problem is the inclusion of a library I made myself, called libCCCI.so into Qt4. I use eclipse Helios on a Ubuntu 10.04. The library libCCCI.so is in a folder called libs within my eclipse project.

My .pro file looks like this:
TEMPLATE = app
TARGET = EPSI
QT += core gui
HEADERS += epsi.h
SOURCES += main.cpp \
epsi.cpp
FORMS += epsi.ui
RESOURCES +=
INCLUDEPATH += ./libs
LIBS += -lCCCI

(I proved all set of combinations in the LIBS parameter, -Lfull path -l library, -Lrelative path -l library etc, but only got replicated -l entries in the g++ line and no solution)

The resultant g++ call is:
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -Ilibs -Idebug -I. -o debug/main.o main.cpp

So the directory is included (-Ilibs) but for some reason Qt4 does not see it. I also copied the library to the root of the project since it is also included (-I.) to see if it saw it there. It did not.

I also added the library in the project->properties->C/C++ project paths->Libraries
And the "libs" path in project->properties->C/C++ include paths and symbols

I run make clean, qmake, make and it gives the next errors:
For the include of header files from the library: No such file or directory
For the objects of the library: Does not name a type.

If I run the project regardless of the errors, I get the following message:
error while loading shared libraries: libCCCI.so: cannot open shared object file: no such file or directory
This is what makes me angry. It knows it is there. Somewhere. But it does not see it.

Thanks to anyone who even bother reading this. I've already lost 4 days of work with this issue and I'm starting to get desperate (and so is my boss). If anyone could at least tell me if there is a problem with my pro file, that would be great.

stampede
2nd February 2011, 17:50
For the include of header files from the library: No such file or directory
Where is this header located ? In the "libs" directory ?
If you want to use shared library in C app, you'll need header as well, not only .so file.

Try to:
0) fix the include .h error, this one has nothing to do with the fact that you are using .so file
1) modify LD_LIBRARY_PATH to contain directory with .so library
2) if it fails try to load it dynamically ( with dlopen("/path/to/lib.so") ) and eventually check error ( dlerror() )

-----
edit:
now I saw this

INCLUDEPATH += ./libs
is this just a typo in your post ? or you have this line in .pro file ?
Did you mean

INCLUDEPATH += . libs ? :)

Dario
3rd February 2011, 09:18
God bless you with 10 fat pink children, stampede.

I had indeed two problems, first the lack of the header files (I thought it was enough with the .so file, since the .h can be found within it, I was wrong). I copied them into the libs folder and that solved the linking and compilation problems.

There was still the running time problem. It still said the library could not be found. That was solved by modifying the LD_LIBRARY_PATH variable.

I have yet one more doubt. If that application is supposed to be portable, in every computer I'll have to export that variable manually first? Is there any way to do it through the eclipse Qt plugin so that whenever the program is called, the variable is automatically exported?

Added after 12 minutes:

Ok, from Eclipse that variable must be set within the Run configurations -> Environment.
Setting the LD_LIBRARY_PATH in the project->properties->C/C++ Make Project->Environment did not work.

Now I only need to find a way to export that variable from the compilation steps, so that from outside Eclipse the program can be executed without running the export LD_LIBRARY_PATH command. Until I find such a solution, I'll use this:

#!/bin/bash
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./libs
./program

stampede
3rd February 2011, 11:41
Now I only need to find a way to export that variable from the compilation steps
Have you considered using the "/usr/local" directory ? I think LD_LIBRARY_PATH should already contain "/usr/local/lib", so you can just put your .so file there without need to modify the path. The same applies to your library header ( "/usr/local/include" ).

Dario
3rd February 2011, 12:06
No luck.
LD_LIBRARY_PATH is empty, so moving the library to /usr/local/lib is not helpful (the headers are not needed in execution time, so those are not required to be copied).

Also I read an interesting article named "Why LD_LIBRARY_PATH is bad".
http://xahlee.org/UnixResource_dir/_/ldpath.html
Which motivates me to find a solution to this without using that variable.
I'll try to find a solution to avoid using this variable. If I find it, I'll post it here. Also if anyone has a clue and can post it would be great.

SixDegrees
3rd February 2011, 14:12
If you're building on Linux, qmake will use the 'RPATH' solution and eliminate the need for setting LD_LIBRARY_PATH; Google 'rpath' for more information.

The problem seems to be your project file, which is a mess. You seem to be confusing the include path and the library path; the first is needed by the compiler, the second by the linker. No matter what solution you use, the linker still needs to know where to find the libraries your application needs so it can examine their symbol tables.

Start by reading through the qmake documentation at http://doc.qt.nokia.com/latest/qmake-manual.html - all of it.

sevenjay
16th February 2011, 06:45
If you're building on Linux, qmake will use the 'RPATH' solution and eliminate the need for setting LD_LIBRARY_PATH; Google 'rpath' for more information.


Thank you.
I do use "-L.. -l.." to indicate the library path like this:

LIBS += -Lxxx/xxx/lib -lxxx
It was complied fine, but run error while loading shared libraries
.
According your suggestion, I add this:

LIBS += -Wl,-rpath,xxx/xxx/lib
It runs fine.