Results 1 to 5 of 5

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

  1. #1
    Join Date
    Nov 2009
    Location
    Greece
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Multiple executable targets sharing common code. Can they be packed into one project?

    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
    Qt Code:
    1. TEMPLATE = subdirs
    2. SUBDIRS = cli gui
    3. cli.file = qspeedtestcli.pro
    4. gui.file = qspeedtestgui.pro
    5. OTHER_FILES += resources/config.ini \
    6. README.txt
    To copy to clipboard, switch view to plain text mode 
    In the same directory, qspeedtestcli.pro
    Qt Code:
    1. QT -= gui
    2. TARGET = qspeedtestcli
    3. TEMPLATE = app
    4. CONFIG += console
    5. CONFIG -= app_bundle
    6. SOURCES += main_qspeedtestcli.cpp \
    7. qspeedtestcli.cpp \
    8. sharedclass.cpp
    9. HEADERS += qspeedtestcli.h \
    10. sharedclass.h
    11. RESOURCES += resources/resources.qrc
    To copy to clipboard, switch view to plain text mode 
    In the same directory, qspeedtestgui.pro
    Qt Code:
    1. TARGET = qspeedtest
    2. TEMPLATE = app
    3. SOURCES += main_qspeedtest.cpp \
    4. qspeedtest.cpp \
    5. sharedclass.cpp \
    6. mainwindow.cpp
    7. HEADERS += qspeedtest.h \
    8. sharedclass.h \
    9. mainwindow.h
    10. FORMS += mainwindow.ui
    11. RESOURCES += resources/resources.qrc
    To copy to clipboard, switch view to plain text mode 
    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:

    qspeedtest.zip

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

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple executable targets sharing common code. Can they be packed into one proj

    Why so complicated?
    Haven't your heard of a library?
    Thats is what they are for!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2009
    Location
    Greece
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple executable targets sharing common code. Can they be packed into one proj

    Quote Originally Posted by high_flyer View Post
    Why so complicated?
    Haven't your heard of a library?
    Thats is what they are for!
    Hello high_flyer! Yes, I indeed have, but:

    Quote Originally Posted by parsifal View Post
    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?

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Multiple executable targets sharing common code. Can they be packed into one proj

    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).

  5. #5
    Join Date
    Nov 2009
    Location
    Greece
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple executable targets sharing common code. Can they be packed into one proj

    With some more persistent reading of the Qt docs and the help of this older post's example attachment, I finally managed to organize my project's code using a shared library.

    Thank you guys!

Similar Threads

  1. QT Project source code
    By darpan in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2012, 16:16
  2. How do I make my project executable file
    By theios_nikos in forum Newbie
    Replies: 8
    Last Post: 4th June 2010, 20:48
  3. code for new project
    By rk0747 in forum Qt Programming
    Replies: 10
    Last Post: 18th February 2010, 20:44
  4. Replies: 4
    Last Post: 17th October 2009, 22:31
  5. Multiple Targets with QMAKE
    By nightghost in forum Qt Tools
    Replies: 1
    Last Post: 7th June 2009, 09:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.