42

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.

demonplus
  • 5,613
  • 12
  • 49
  • 68
Dan O
  • 4,323
  • 5
  • 29
  • 44

5 Answers5

39
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
  • 23,277
  • 13
  • 73
  • 121
  • 1
    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 Sep 01 '09 at 08:52
  • 5
    This didn't work for me but another answer here did, which is right clicking while editing the .pro file and using the "Add Library" UI. – andrewrk Sep 16 '13 at 18:05
  • Don't forget to add the path of the library's headers if these are not in the same directory than the library. In .pro file, add INCLUDEPATH += [path to library's headers] – Uglylab Feb 24 '23 at 08:15
19

The variant

 LIBS += -L[PATH_TO_LIB_DIR] -l[LIBNAME] 

doesn't work if you have both static libLIBNAME.a and dynamic libLIBNAME.so libs in the same folder PATH_TO_LIB_DIR.
In this case on my linux with QMake v 3.0 the dynamic one is linked by default.
To force linkage with static one you need to specify it explicitly without any options.

LIBS += PATH_TO_LIB_DIR/libLIBNAME.a
Temak
  • 2,929
  • 36
  • 49
  • I noticed something simliar: If there is a `module.dll` and a `libmodule.a` in the same folder and I include the dll with `-L -l`, I get a multiple definitions error. However, if I delete the static lib file (.a), then it works. Not sure if this was a coincidence or expected behaviour... – user2366975 Jan 12 '17 at 09:43
  • 2
    This is the only place someone has explained how to force linker to use .a if .so is found in same directory – TSG Jun 28 '20 at 18:29
  • This is the only answer that helped getting static linkage of a library. Thanks! – silverdr May 12 '21 at 19:26
19

..from QT project creator

  1. goto projectName.pro from left hand side menu
  2. type LIBS +=
  3. rightClick AddLibrary
  • 11
    typing `LIBS +=` is not necessary. You can shorten this to 2 steps. – andrewrk Sep 16 '13 at 18:04
  • Actually this is the only solution I could make work out of all the answers. As andrewrk suggested, you don't need to type LIBS +=. All you need is to go to the .pro file, right click, Add Library, and let QtCreator generate the required lines of code (which BTW, on my system [Qt 5.4, Linux], looked fairly different from what was suggested in other answers.) – Karpov Jul 03 '15 at 16:05
  • Despite doing this, and selecting the correct _static_ library I still kept getting dynamic one linked. The answer by Temac helped. – silverdr May 12 '21 at 19:24
18

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.

Community
  • 1
  • 1
rpg
  • 7,746
  • 3
  • 38
  • 43
  • Thank you for posting this answer. Here are the links to LIBS and INCLUDEPATH variables from Qt5's docs: [LIBS](http://doc.qt.io/qt-5/qmake-variable-reference.html#libs), and [INCLUDEPATH](http://doc.qt.io/qt-5/qmake-variable-reference.html#includepath). – RAM Jan 30 '18 at 09:30
2

Is it

LIBS += -L"/some path" -l"somename.a"

or

LIBS += -L/somepath -lsomename.a

or

LIBS += -L/somepath -lsomename"

This should be as easy as it gets but for some reason it is EXTREMELY hard to pull up a search result for because there are so many hits of forums of people asking for help and I've followed every tip I can get but no help...

Coren
  • 5,517
  • 1
  • 21
  • 34
Know-One
  • 29
  • 2