Home > Back-end >  How do you link libraries in Qt?
How do you link libraries in Qt?

Time:03-16

I have a project that I've written in VS2017 that has a lot of static libraries and I've got to the point where I want to start refining the gui. To make sure I can use Qt I made a test subdir program using the tips in https://www.toptal.com/qt/vital-guide-qmake, the https://wiki.qt.io/SUBDIRS_-_handling_dependencies example, the https://github.com/rainbyte/example-qmake-subdirs and https://github.com/219-design/qt-qml-project-template-with-ci/tree/6af6488d74e1dc97a8cef545080074055a696e9a projects as well as several of the similar questions here on SO. This consists of a subdir project called "project" with 2 sub-projects "app" and "library". These are both simple QtApplications created using the New Subproject wizard, with all the files except for main.cpp removed from "app", everything left in except for main.cpp in "library", the point being to make sure mainwindow.h in "library" is referenced from main.cpp in "app". Despite trying every example I can find I'm still getting the "'mainwindow.h' file not found" error.

According to all the examples I could find you should only need to add a few lines (#) to the wizard-produced .pro files and add an additional .pri file to the "library" project;

Directory structure;

project/
    project.pro 
    app/
        app.pro
        main.ccp
    library/
        library.pro
        mainwindow.h
        mainwindow.ccp
        mainwindow.ui
        library.pri

project.pro;

TEMPLATE = subdirs
TARGET = project ##

SUBDIRS  = \
    app \
    library

app.depends = library ##

app.pro;

TEMPLATE = app ##
TARGET = app ##

QT        = core gui

greaterThan(QT_MAJOR_VERSION, 4): QT  = widgets

CONFIG  = c  11

SOURCES  = \
    main.cpp

include(../library/library.pri) ##

library.pro;

TEMPLATE = lib ##
TARGET = library ##

QT        = core gui

greaterThan(QT_MAJOR_VERSION, 4): QT  = widgets

CONFIG  = c  11
CONFIG  = shared ##
CONFIG  = lib_bundle ##

SOURCES  = \
    mainwindow.cpp

HEADERS  = \
    mainwindow.h

FORMS  = \
    mainwindow.ui

DISTFILES  = \
    library.pri ##

and the added library.pri file;

LIBTARGET = library
BASEDIR   = $${PWD}
INCLUDEPATH *= $${BASEDIR}/include
LIBS  = -L$${DESTDIR} -llibrary

I know this has been asked many times already but I can find no definitive solution, or rather no-one seems to have bothered to post their working solution. I'd really appreciate any help with this, I'd love to be able to use Qt and escape the purgatory of win32 ui creation.

CodePudding user response:

You have this:

INCLUDEPATH *= $${BASEDIR}/include

But you don't have directory named include anywhere, it seems. So probably remove the /include part from above.

  • Related