PDA

View Full Version : linking libraries



JustaStudent
24th March 2006, 11:43
Hi,

I've used Qt for two days now, as I need it to make my final project (exam).

I am trying to link a library called "Vortex" to my Qt-project.

First I generate the Makefile;

qmake -o Makefile client

I've appended 'pkg-config vortex --cflags' to CFLAGS and CXXFLAGS in the Makefile;

CFLAGS = <previous flags>... 'pkg-config vortex --cflags'
CXXFLAGS = <previous flags>... 'pkg-config vortex --cflags'

and also added 'pkg-config vortex --libs' to the Makefile, in the LIBS section;

LIBS = <previous libs>... 'pkg-config vortex --libs'

Then I try to compile the code with "make".
This only results in an error message;

undefined reference to 'vortex_init()'

(vortex_init() is a function to initialize the Vortex library)

I have used #include <vortex.h> in source-file, client.cpp.




Am I perhaps linking the library the wrong way?

wysota
24th March 2006, 13:18
You should probably implement vortex_init() function yourself. Or is it contained in the library? Do you get other unresolved symbols too or is it the only one?

JustaStudent
24th March 2006, 14:19
this is the only error, but it is also the only function I have in client.cpp for now.

/usr/local/include/vortex.h contains

gboolean vortex_init ();

I don't really know how to implement this function in my client.cpp file.. :confused:

Thanks for the answer! :)


output from compiler when running "make"


g++ -c -pipe -Wall -W -O2 -D_REENTRANT -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -DQT_SHARED -DQT_TABLET_SUPPORT `pkg-config vortex --cflags` -I/usr/share/qt3/mkspecs/default -I. -I/usr/include/qt3 -I.ui/ -I.moc/ -o .obj/client.o client.cpp
/usr/include/qt3/private/qucom_p.h:69: warning: ‘struct QUBuffer’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:77: warning: ‘struct QUType’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:104: warning: ‘struct QUType_Null’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:287: warning: ‘struct QUType_enum’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:307: warning: ‘struct QUType_ptr’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:326: warning: ‘struct QUType_iface’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:345: warning: ‘struct QUType_idisp’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:364: warning: ‘struct QUType_bool’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:383: warning: ‘struct QUType_int’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:403: warning: ‘struct QUType_double’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:423: warning: ‘struct QUType_charstar’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:444: warning: ‘struct QUType_QString’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucomextra_p.h:65: warning: ‘struct QUType_QVariant’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucomextra_p.h:87: warning: ‘struct QUType_varptr’ has virtual functions but non-virtual destructor
g++ -o client .obj/client.o -L/usr/share/qt3/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11 -lm -lpthread `pkg-config vortex --libs`
.obj/client.o: In function `main':
client.cpp:(.text+0x2a6): undefined reference to `vortex_init()'
collect2: ld returned 1 exit status
make: *** [client] Error 1



this is what I would need to run in order to get client.o compiled the right way:


g++ -c -pipe -Wall -W -O2 -D_REENTRANT -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -DQT_SHARED -DQT_TABLET_SUPPORT `pkg-config --libs vortex --cflags` -I/usr/share/qt3/mkspecs/default -I. -I/usr/include/qt3 -I.ui/ -I.moc/ -o .obj/client.o client.cpp


but that only generates more error messages;


g++: -lvortex: linker input file unused because linking not done
g++: -lgthread-2.0: linker input file unused because linking not done
g++: -lxml2: linker input file unused because linking not done
g++: -lz: linker input file unused because linking not done
g++: -lm: linker input file unused because linking not done
g++: -lglib-2.0: linker input file unused because linking not done

wysota
24th March 2006, 17:16
Have you used that library before? Isn't there some reference which tells you what needs to be done to use it?

JustaStudent
24th March 2006, 17:38
yup, have used it before in an application not invloving Qt. That application works just fine to compile with the instructions given at;

http://www.aspl.es/fact/files/af-arch/vortex/html/install.html#using_linux

i.e. compile the source file with


gcc `pkg-config --cflags --libs vortex` your-program.c -o your-program

I think the problem may have something to do with the fact that Qt firstly compile my client.cpp to .obj/client.o and secondly compile the .o file;


g++ <FLAGS..> -o .obj/client.o client.cpp

g++ -o client .obj/client.o <FLAGS...>

jacek
24th March 2006, 17:43
From Qt Designer docs:
For example, qmake can configure the build process to take advantage of external libraries that are supported by pkg-config, such as the D-BUS and ogg libraries, with the following lines:
CONFIG += link_pkgconfig
PKGCONFIG += ogg dbus-1
More information about features can be found in the Adding New Configuration Features section of the qmake Advanced Usage chapter.

JustaStudent
25th March 2006, 10:59
That solution is for Qt 4.x, and I'm using Qt 3.3.4. (I found it in the 4.x manual, but not in the 3.3 manual)

http://doc.trolltech.com/4.0/qmake-project-files.html#configuration-features

I tried to use this solution in my Qt 3.3.4, but qmake just ignores it, and makes the Makefile as it would be without

CONFIG += link_pkgconfig
PKGCONFIG += vortex

(I added the lines above to client.pro, then run qmake -o Makefile client)

Perhaps an upgrade to 4.x will do it then. I will try to install it and try this out! I'll post comments about this later.

wysota
25th March 2006, 12:39
Could you show us what parameters get passed to the compiler in the linking stage?

jacek
25th March 2006, 12:58
That solution is for Qt 4.x, and I'm using Qt 3.3.4. (I found it in the 4.x manual, but not in the 3.3 manual)

Here's the contents of $QTDIR/mkspecs/features/link_pkgconfig.prf file from Qt4:
# handle pkg-config files
for(PKGCONFIG_LIB, $$list($$unique(PKGCONFIG))) {
QMAKE_CXXFLAGS += $$system(pkg-config --cflags $$PKGCONFIG_LIB)
LIBS += $$system(pkg-config --libs $$PKGCONFIG_LIB)
}
It should work with Qt3 also.

JustaStudent
25th March 2006, 15:21
jacek, this solution works also for Qt 3.3.4. (with a little modification)

I edit /usr/share/qt3/mkspecs/default/qmake.conf (after making backup) and the lines

QMAKE_CFLAGS = -pipe

to

QMAKE_CFLAGS = -pipe `pkg-config vortex --cflags`

and

QMAKE_LIBS =

to

QMAKE_LIBS =`pkg-config vortex --libs`


save it, run qmake -o Makefile client again, and then make.

but still I get the same error message:


g++ -o client .obj/client.o -L/usr/share/qt3/lib -L/usr/X11R6/lib `pkg-config vortex --libs` -lqt-mt -lXext -lX11 -lm -lpthread
.obj/client.o: In function `main':
client.cpp:(.text+0x2a6): undefined reference to `vortex_init()'
collect2: ld returned 1 exit status
make: *** [client] Error 1


wysota, what do you mean with 'parameters passed to the compiler in the linking stage'?
After above error, it stops compiling..

wysota
25th March 2006, 15:58
What does pkg-config vortex --libs return?

JustaStudent
25th March 2006, 16:14
pkg-config vortex --libs
-pthread -L/usr/local/lib -lvortex -lgthread-2.0 -lxml2 -lz -lm -lglib-2.0

pkg-config vortex --cflags
-pthread -I/usr/local/include/vortex -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libxml2


I'll wait with the install of Qt 4.1.1 until I know for sure it won't mess up my current configuration.

jacek
25th March 2006, 16:46
I edit /usr/share/qt3/mkspecs/default/qmake.conf (after making backup) and the lines
It would be better if you would add:
QMAKE_CXXFLAGS += $$system(pkg-config --cflags vortex)
LIBS += $$system(pkg-config --libs vortex)to your .pro file.

JustaStudent
25th March 2006, 18:54
ok I restored my qmake.conf file and added

QMAKE_CXXFLAGS += $$system(pkg-config --cflags vortex)
LIBS += $$system(pkg-config --libs vortex)

to my .pro file

but still I get the same error message "undefined reference to vortex_init()"

Any ideas?

jacek
25th March 2006, 19:18
Have you run "make clean && qmake && make" after all these changes?

JustaStudent
25th March 2006, 19:54
> more dummy.cpp


#include <qapplication.h>
#include <vortex.h>

int main( )
{
vortex_init();
}


> more dummy.pro


TEMPLATE = app
LANGUAGE = C++

CONFIG += qt warn_on release

SOURCES += dummy.cpp

FORMS = form1.ui

QMAKE_CXXFLAGS += $$system(pkg-config --cflags vortex)
LIBS += $$system(pkg-config --libs vortex)

unix {
UI_DIR = .ui
MOC_DIR = .moc
OBJECTS_DIR = .obj
}


> make clean && qmake && make


rm -f .obj/moc_form1.o
rm -f .moc/moc_form1.cpp
rm -f .ui/form1.cpp .ui/form1.h
rm -f .obj/dummy.o .obj/form1.o
rm -f *~ core *.core
/usr/share/qt3/bin/uic form1.ui -o .ui/form1.h
g++ -c -pipe -pthread -I/usr/local/include/vortex -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libxml2 -Wall -W -O2 -D_REENTRANT -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -DQT_SHARED -DQT_TABLET_SUPPORT -I/usr/share/qt3/mkspecs/default -I. -I/usr/include/qt3 -I.ui/ -I. -I.moc/ -o .obj/dummy.o dummy.cpp
/usr/share/qt3/bin/uic form1.ui -i form1.h -o .ui/form1.cpp
g++ -c -pipe -pthread -I/usr/local/include/vortex -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libxml2 -Wall -W -O2 -D_REENTRANT -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -DQT_SHARED -DQT_TABLET_SUPPORT -I/usr/share/qt3/mkspecs/default -I. -I/usr/include/qt3 -I.ui/ -I. -I.moc/ -o .obj/form1.o .ui/form1.cpp
/usr/include/qt3/qtooltip.h:86: warning: ‘class QToolTip’ has virtual functions but non-virtual destructor
/usr/share/qt3/bin/moc .ui/form1.h -o .moc/moc_form1.cpp
g++ -c -pipe -pthread -I/usr/local/include/vortex -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libxml2 -Wall -W -O2 -D_REENTRANT -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -DQT_SHARED -DQT_TABLET_SUPPORT -I/usr/share/qt3/mkspecs/default -I. -I/usr/include/qt3 -I.ui/ -I. -I.moc/ -o .obj/moc_form1.o .moc/moc_form1.cpp
/usr/include/qt3/private/qucom_p.h:69: warning: ‘struct QUBuffer’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:77: warning: ‘struct QUType’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:104: warning: ‘struct QUType_Null’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:287: warning: ‘struct QUType_enum’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:307: warning: ‘struct QUType_ptr’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:326: warning: ‘struct QUType_iface’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:345: warning: ‘struct QUType_idisp’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:364: warning: ‘struct QUType_bool’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:383: warning: ‘struct QUType_int’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:403: warning: ‘struct QUType_double’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:423: warning: ‘struct QUType_charstar’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucom_p.h:444: warning: ‘struct QUType_QString’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucomextra_p.h:65: warning: ‘struct QUType_QVariant’ has virtual functions but non-virtual destructor
/usr/include/qt3/private/qucomextra_p.h:87: warning: ‘struct QUType_varptr’ has virtual functions but non-virtual destructor
g++ -o dummy .obj/dummy.o .obj/form1.o .obj/moc_form1.o -L/usr/share/qt3/lib -L/usr/X11R6/lib -pthread -L/usr/local/lib -lvortex -lgthread-2.0 -lxml2 -lz -lglib-2.0 -lqt-mt -lXext -lX11 -lm -lpthread
.obj/dummy.o: In function `main':
dummy.cpp:(.text+0xd): undefined reference to `vortex_init()'
collect2: ld returned 1 exit status
make: *** [dummy] Error 1

jacek
25th March 2006, 20:24
Does it link when you remove #include <qapplication.h> and change:
CONFIG += qt warn_on releaseto
CONFIG += warn_on release
CONFIG -= qt?

wysota
25th March 2006, 20:42
// main.cpp
#include <vortex.h>

void main( ){
vortex_init();
}


g++ main.cpp `pkg-config vortex --cflags --libs` -o test

Does this compile?

JustaStudent
25th March 2006, 20:55
I found the problem! It is my bad actually, the Vortex library is a C library, not a C++ library.
So,

I changed the name of dummy.cpp to dummy.c,
I changed CXXFLAGS to CFLAGS in dummy.pro as below, then run

make clean && qmake && make.

Code compiled! I feel stupid that I did not realise this earlier! :o
Thank you for the troubleshooting!

> more dummy.c


#include <vortex.h>

int main( )
{
vortex_init();
}



> more dummy.pro


TEMPLATE = app
LANGUAGE = C++

CONFIG += qt warn_on release

SOURCES += dummy.c

LIBS += $$system(pkg-config --libs vortex)

QMAKE_CFLAGS += $$system(pkg-config --cflags vortex)

unix {
UI_DIR = .ui
MOC_DIR = .moc
OBJECTS_DIR = .obj
}





> make clean && qmake && make


qmake -o Makefile dummy.pro
rm -f .obj/dummy.o
rm -f *~ core *.core
gcc -c -pipe -pthread -I/usr/local/include/vortex -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libxml2 -Wall -W -O2 -D_REENTRANT -DQT_NO_DEBUG -DQT_THREAD_SUPPORT -DQT_SHARED -DQT_TABLET_SUPPORT -I/usr/share/qt3/mkspecs/default -I. -I/usr/include/qt3 -I.ui/ -I.moc/ -o .obj/dummy.o dummy.c
dummy.c: In function ‘main’:
dummy.c:6: warning: control reaches end of non-void function
g++ -o dummy .obj/dummy.o -L/usr/share/qt3/lib -L/usr/X11R6/lib -pthread -L/usr/local/lib -lvortex -lgthread-2.0 -lxml2 -lz -lglib-2.0 -lqt-mt -lXext -lX11 -lm -lpthread

jacek
25th March 2006, 21:19
I found the problem! It is my bad actually, the Vortex library is a C library, not a C++ library.
Then you should use Vortex headers like this:

extern "C" {
#include <vortex.h>
};
Athough it's strange that Vortex authors didn't take care of it.

JustaStudent
25th March 2006, 21:49
Right. Thanks again for the help! Youv'e saved me some days to finish a prototype of the project :)

(I renamed file to dummy.cpp and used CXXFLAGS again in dummy.pro, then put extern "C" in dummy.cpp as you mentioned & recompiled)

JustaStudent
18th April 2006, 16:45
Ok, now to the next problem.. This should fit under the same topic.

I want to build a static executable with this vortex library included. I have tried putting "static" in my "CONFIG +=" in my .pro file, but that just makes no difference. (same size of executable, and not working on other machine with same distro)

jacek
18th April 2006, 21:42
Do you have a static version of that library?

JustaStudent
19th April 2006, 09:21
no, not that i know of.
the library is compiled with --enable-static but I dont know where the static library is installed..

jacek
19th April 2006, 14:13
If you don't have a static version of that library you can't link statically with it. Look for libvortex.a or something similar on your filesystem.

JustaStudent
20th April 2006, 09:27
Found it!

/usr/local/lib/libvortex.a

how do i now compile my Qt project with this statically?

(thanks for the help, btw! you seem to really know this stuff.)

jacek
20th April 2006, 11:23
If you use pkg-config, then try changing --libs to --static.

JustaStudent
26th April 2006, 13:26
I changed the libs in project settings from "$$system(pkg-config --libs vortex)" to "--static $$system(pkg-config --libs vortex)"

Now I get this error message;

/usr/bin/ld: cannot find -lqt-mt

jacek
26th April 2006, 15:06
Now I get this error message;

/usr/bin/ld: cannot find -lqt-mt
It looks like it tries to link statically with Qt.

Try:
-static $$system(pkg-config --libs vortex) -dy

JustaStudent
2nd May 2006, 08:30
I tried the -dy switch, still the same. I want also Qt libraries to be static. I have /usr/local/qt/lib/libqt-mt.a already so that should be possible