PDA

View Full Version : Multiple executable targets sharing common code. Can they be packed into one project?



parsifal
30th June 2010, 13:13
Hello!

As per title, I'm working on a project, trying to get 2 executable targets (one GUI, the other console) which share some common code. What I have done so far:

Top project file, qspeedtest.pro


TEMPLATE = subdirs
SUBDIRS = cli gui
cli.file = qspeedtestcli.pro
gui.file = qspeedtestgui.pro
OTHER_FILES += resources/config.ini \
README.txt

In the same directory, qspeedtestcli.pro


QT -= gui
TARGET = qspeedtestcli
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
SOURCES += main_qspeedtestcli.cpp \
qspeedtestcli.cpp \
sharedclass.cpp
HEADERS += qspeedtestcli.h \
sharedclass.h
RESOURCES += resources/resources.qrc

In the same directory, qspeedtestgui.pro


TARGET = qspeedtest
TEMPLATE = app
SOURCES += main_qspeedtest.cpp \
qspeedtest.cpp \
sharedclass.cpp \
mainwindow.cpp
HEADERS += qspeedtest.h \
sharedclass.h \
mainwindow.h
FORMS += mainwindow.ui
RESOURCES += resources/resources.qrc

The above is a (somewhat) minimal example of the actual contents of my project files. As you can see, there is some common code that the 2 subprojects share, represented here by the class files sharedclass.h and sharedclass.cpp. In the actual code, there are more classes being utilized in both executables. For the convenience of anyone wishing to examine the rest of the files, here is a ZIP archive containing the above project with some skeleton code. It is perfectly compilable:

4851

The above layout works but
1. It gives a horridly redundant view of the file hierarchy inside Qt Creator:
4852

2. It just doesn't feel right!


So my question is: Is there a better and/or more proper way to design and organize this project's code? I was thinking that maybe creating a shared dynamic library that holds the common code would be a nice approach, but I have yet to locate a thorough "From A-Z" example demonstrating this approach. And my own attempts with the help of only the Qt documentation proved inadequate. :(


Any thoughts or advice? Thanks in advance!

high_flyer
30th June 2010, 13:49
Why so complicated?
Haven't your heard of a library?
Thats is what they are for!

parsifal
30th June 2010, 14:58
Why so complicated?
Haven't your heard of a library?
Thats is what they are for!

Hello high_flyer! Yes, I indeed have, but:


I was thinking that maybe creating a shared dynamic library that holds the common code would be a nice approach, but I have yet to locate a thorough "From A-Z" example demonstrating this approach. And my own attempts with the help of only the Qt documentation proved inadequate. :(

Could you provide me with a small example please? :)

SixDegrees
30th June 2010, 15:18
Look through the qmake documentation on how to create libraries, either static or dynamic. It isn't much harder than saying 'TEMPLATE = lib' in your project file, and adding your resulting library to the LIBS variable where you build your application(s).

parsifal
2nd July 2010, 21:16
With some more persistent reading of the Qt docs and the help of this older post's (http://www.qtcentre.org/threads/1201-Using-dll-in-QT4?p=7122#post7122) example attachment, I finally managed to organize my project's code using a shared library.

Thank you guys! :)