chacham15
chacham15

Reputation: 14251

How do I build a static library and executable with Qt?

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

How do I set this up in the qmake file?

Upvotes: 33

Views: 41978

Answers (2)

thedebugger
thedebugger

Reputation: 91

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

Upvotes: 1

Masci
Masci

Reputation: 6074

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

Upvotes: 40

Related Questions