32

To simplify the situation, lets say that there are 2 files: core.cpp and main.cpp.

core.cpp contains the functionality of the program and main.cpp contains the basic main() implementation.

I want Qt (using qmake and the .pro files) to

  • first build core.a and then
  • use that and main.cpp to build main.exe.

How do I set this up in the qmake file?

menjaraz
  • 7,551
  • 4
  • 41
  • 81
chacham15
  • 13,719
  • 26
  • 104
  • 207

2 Answers2

40

Filesystem layout:

MyProject
|_ myproject.pro
|_ core
   |_ core.cpp
   |_ core.h
   |_ core.pro
|_ app
   |_ main.cpp
   |_ app.pro

myproject.pro:

TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS = core \
          app
app.depends = core

core.pro:

TEMPLATE = lib
CONFIG += staticlib
HEADERS = core.h
SOURCES = core.cpp

app.pro:

TEMPLATE = app
SOURCES = main.cpp
LIBS += -L../core -lcore
TARGET = ../app-exe # move executable one dire up
mrts
  • 16,697
  • 8
  • 89
  • 72
Masci
  • 5,864
  • 1
  • 26
  • 21
  • Nitpick: Pressing play results in: `Failed to start program. Path or permissions wrong?`. How do i fix this? (It apparently is looking for the executable in the base directory) – chacham15 Apr 05 '12 at 22:38
  • Just move built executable one dir up with TARGET var, see updated answer – Masci Apr 06 '12 at 07:34
  • 4
    `INCLUDEPATH += ../core/` should also be required in `app.pro`, right? – Jorge Leitao May 31 '16 at 04:35
  • 2
    How to handle the case where `core.h` includes something from a particular Qt library, e.g. `QT += websockets`? We don't want `app.pro` to have to declare explicit dependencies on libraries that are only pulled in indirectly... – Thomas Aug 31 '16 at 09:24
1

If you are utilizing resources in your static library you should import them in your application as well. Q_INIT_RESOURCE is the way of importing a resource file into the application.

Assume that you have a resources file with file name as myResources.qrc in static library. Then, you should initialize this in the app as given below:

QApplication a(argc, argv);

Q_INIT_RESOURCE(resources); //Magic is here.

MainWindow w;
w.show();
a.exec();

The .pro file might be modified as given below for the great example given by Masci:

TEMPLATE = lib
CONFIG += staticlib
HEADERS = core.h
SOURCES = core.cpp
RESOURCES += myResources.qrc