Important: Please read the Qt Code of Conduct -

how to statically link .a to qt (solved)

  • i have a static file called libQLoglib.a, like to know how to statically link and use the library.
    I just inserted the line below into project file
    LIBS +=-l libQLogLib

  • @houmingc You don't need to write "lib". I mean, it's enough to write the next line:

    LIBS += -lQLogLib

    If the compiler doesn't find the the lib, you'll need to add the path:

    LIBS += -L<path_to_lib> -lQLogLib

  • In Project, i insert

          INCLUDEPATH += /myDir                        //where myfile.a is
          LIBS += -l myfile.a
    My IDE error:
    cannot find Lib
    collect2: error: ld returned 1 exit status

  • @houmingc Hmmm... I think it sould be a code like this:

    LIBS += -L./myDir -lmyfile

    INCLUDEPATH is to find include files (.h) and LIBS to find libraries and its paths.

    Don't write a blank space between -l and your lib or between -L and your lib path.

    BTW, following your example, it seems myDir is in the root filesystem. Is that right? I suppose no.

  • Files is not in root file system. Below is what i did:

    Compiler error:
    cannot find -llibQLogLib
    error: ld returned 1 exit address

    Assuming libQLogLib.a is in folder home/git1/TrainKochi33
    I insert the line below in Pro file

    LIBS += -L/home/git1/TrainKochi33 -llibQLogLib
    LIBS += -llibQLogLib

    I insert all the header files and use the function in the lib as per normal.
    Then i rebuild, run qmake and run.
    Please kindly assist, what when wrong.

  • @houmingc As I said before, you don't need to put the text "lib" if the library begins with those letters.

    So in your case, you should write the next line:

    LIBS += -L/home/git1/TrainKochi33 -lQLogLib

  • Sir, static library name itself is libQLogLib.
    Initially a third-party pass me .c and .h files
    I compile them with error, in the end i ask them for .a file.
    Now i can't static link them into my GUI.

    Below is my pro file

    QT          += core gui
    QT          += network
    QT          +=gui
    QT          +=gui declarative
    QT       += widgets
    QT       += multimedia
    QT       += multimediawidgets
    QT       += xml
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    TARGET = TrainKochi33
    TEMPLATE = app
    CONFIG += c++11
    #CONFIG += -static libQLogLib
    win32:LIBS += -lrt
    SOURCES += main.cpp\
            mainwindow.cpp \
        threada.cpp \
        mytimer.cpp \
        functions.cpp \
    HEADERS  += mainwindow.h \
        threada.h \
        mytimer.h \
        functions.h \
        i2c.h \
        includes.h \
        CLogMsgQ.h \
        CMsgQ.h \
    #INCLUDEPATH += libQLogLib.a
    LIBS += -L\home\git1\TrainKochi33 -llibQLogLib
    FORMS    += mainwindow.ui
    #DISTFILES += \
    #    libQLogLib.a
    #INCLUDEPATH += /home/git1/TrainKochi33
    #CONFIG += libQLogLib.a

  • I try another method, documented below:-

    Add Library <Right-clip Project file>
    Select External library, In Library File, select libQLogLib.a
    run qmake
    error: cannot find -lQLoglib

  • @houmingc Perhaps I didn't explain myself properly.

    If the library is named libXXX.a or, you don't need to put the lib prefix in your qmake or Makefile.

    More info here:

    Besides, you pro file has a wrong line. Change this one:

    LIBS += -L\home\git1\TrainKochi33 -llibQLogLib

    And use this one instead:

    LIBS += -L/home/git1/TrainKochi33 -lQLogLib

  • Hi, I do it this way (i'm working on unix):

    unix:!macx: LIBS += -L$$PWD/../../Project1/Release/ -libOne
    INCLUDEPATH += $$PWD/../../LibOne
    DEPENDPATH += $$PWD/../../LibOne/Release
    unix:!macx: PRE_TARGETDEPS += $$PWD/../../Project1/Release/libOne.a

    I use all this 4 lines for every lib I need. In this case my lib is named "libOne.a" and my paths structure is something like this: ( so you can understand it better)

    workspace (folder)
    |-----QtProject (folder)
    |               |-Myproject (folder)
    |                       |- MyProject.exe  // so from here u have to go up 2 steps (that's why I have   "$$PWD/../../"
    |-----LibOne (Folder)
    |            |-Release (folder)
    |            |      |- libOne.a  (The file I want)
    |            |- Debug (folder) ...

    Hope it helps you.

  • @roseicollis I don't know how Unix works, but at least in Linux, it's not necessary the prefix libwhen you're linking a library named lib<xxx>.so (or .a)


  • neither do I hehe I'm new on this but I had to work on a fedora with Qt and as this worked fine for me, I thought it could help @houmingc so I posted an example of my case. Even if you don't need to put the "lib" it doesn't mean that putting it its wrong and as shown with my example, it works too :) In my case if its possible I prefer to put the entire name or path so its more clear to understand.

    Anyway its always nice to learn new things so thanks for the info and the link! :) I'll keep it in mind the next time

  • @roseicollis Great! Thank you! :)

  • Thanks you. it worked

  • Someone direct me to insert the line below and it solved my problem.
    Can someone explain what this line of code do in Qt project?

    unix|win32: LIBS +=-lrt

  • Lifetime Qt Champion

    @houmingc this line means: on either unix or win32 link to the rt library

  • Thanks.
    Where is rt library in ubuntu?
    How to check if rt library is install in ubuntu and install it? i try " locate librt "
    Why is it not linked automatically?

Log in to reply