views:

4642

answers:

2

Greetings,

I'm having a hell of a time finding documentation which clearly explains how to use a static library in QT Creator.

I've created and compiled my static library using QT Creator (New=>Projects\C++ Library=>Set type to "Statically Linked Library"). It compiles and spits out a ".a file".

The problem I encounter is when I try to use the library. I have another project that would like to use it (#include files in the library, etc) but I don't know the proper way to link with the library or include files from the library.

Any help is appreciated, Dan O.

+2  A: 

In your project that uses the library make the LIBS variable point to your lib's path.
To include files from the library, add the library folder to the INCLUDEPATH and then do a regular #include in your code files.

e.g:

# the binary's .pro  
LIBS += c:/mylibs/math.lib
INCLUDEPATH += c:/mylibs

Edited:
-L tells qmake that the path is a directory that it can search for libraries -l tells it that the path is a file, but take note of the observation below.

From the qmake docs:

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.

Note: On Windows, specifying libraries with the -l option, as in the above example, will cause the library with the highest version number to be used; for example, libmath2.lib could potentially be used instead of libmathlib. To avoid this ambiguity, we recommend that you explicitly specify the library to be used by including the .lib file name suffix.

rpg
+4  A: 
LIBS += -L[path to lib] -l[name of lib]

Note! that filename of lib: lib[nameOfLib].a and you have to pass only original part -l[nameOfLib]

Dewfy
I found the answer at http://doc.trolltech.com/4.5/qmake-project-files.html#declaring-other-libraries shortly after posting it (had been googling stupid stuff about creator rather than just wandering into qmake docs).I still don't know what the -L or -l mean, but such is the nature of makefiles.. command line gobbl-dee-gook that can easily be parsed into... command line gobble-dee-gook.RPG's answer is also relevent, the includepath stuff is important.. I marked yours correct because the -L/-l also seem important!
Dan O