What is the right way to share common code across qt project?



  • This seems fairly simple. But I somehow donot get it working.
    I have 3 projects

    -historical
          -historical.pro
          -main.cpp
           -mainwindow.ui
    
    -live
          -live.pro
          -main.cpp
           -mainwindow.ui
    
    -common
       -common.pro //these are just simple .h and .cpp files , if possible i can do away with creating a qt project
       -common.h
        -common.cpp
    

    Now my goal

    • to consume files of common in both live and historical project.
    • To debug as usual, step into the files of common folder/project as well
    • Continue with life as easy as possible

    Note live and historical are two completely different applications.

    Here is what I have done.
    Step 1:- I went into qt->right click on 'live' project->add existing files->include all the files from common pro.
    Step 2: Every include statement in live or historical which referes to common i state like this

    //#include"../common/common.h"
    

    The build goes through fine but while debugging in QT Creator I am able to step-into the function. Life was just as good , when the debugger says not accessable to the value received from common functions. Wanted to know what is causing this and how can I get away with it?
    I am using

    Qt Creator 4.4.1
    Based on Qt 5.9.2 (MSVC 2015, 32 bit)

    Built on Oct 4 2017 04:12:53

    From revision 6afdb8bdf9


  • Moderators

    @gully said in What is the right way to share common code across qt project?:

    //#include"../common/common.h"
    

    This include statement is commented out. Therefore it shall have no effect in the compilation process. Possibly you are providing the information in this declaration file through some other means.

    You have a couple of possible approaches.

    One possibility is declaring a library with all the source files in common. The library as output has to be linked to the other applications.

    When you have only few files the overhead may be too much and the direct use of the common files in the compilation process is typically faster. However, all depends on your actual plans and likings.

    Anyway whatever you plan to do, you need to ensure that declaration content of common.h is only accessible in common.h. The very same physical common.h has to used in all places.

    Since you are using Qt creator you can ensure and redirect the include path with http://doc.qt.io/qt-5/qmake-variable-reference.html#includepath

    For instance you have

    c:/SourceBase/historical
    c:/SourceBase/live
    c:/SourceBase/common
    

    in all *.pro you can have a statement like

    INCLUDEPATH += ../common
    

    In your source files you can refer to the declaration file simply as

    #include "common.h"
    

    When you like to check different versions of your common source,you can simply change the INCLUDEPATH statement and redirect the include to whereever you like. Note: sometimes it is better to have a complete rebuild then.



  • Hi @gully
    Adding points to @koahnig

    its recommended to create a folder which is common to all the three projects(historical, live, common) i.e., A_PROJECT folder
    A_PROJECT > historical, A_PROJECT > live, A_PROJECT > common

    • A_PROJECT folder
      A_PROJECT > A_PROJECT.pro
    TEMPLATE = subdirs
    CONFIG += ordered
    SUBDIRS = historical \
              live \
              common
    historical.depends = common
    live.depends = common
    
    • A_PROJECT > historical folder
      A_PROJECT > historical > historical.pro
    # requires more statements above 
    INCLUDEPATH += ../common
    # requires more statements below
    
    • A_PROJECT > live folder
      A_PROJECT > live > live.pro
    # requires more statements above 
    INCLUDEPATH += ../common
    # requires more statements below
    
    • A_PROJECT > common folder
      A_PROJECT > common > common .pro

    make common project as shared library & start using in other projects.

    [Edit: koahnig] added code tags for .pro file snippets, because backslash was not shown for SUBDIRS


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.