Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Linking static library to an executable
QtWS25 Last Chance

Linking static library to an executable

Scheduled Pinned Locked Moved Solved General and Desktop
static librarystatic linkingexecutableqmake
11 Posts 2 Posters 28.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E enmaniac

    Hi all,

    I am struggling with quite simple problem.
    I do have 2 projects in a session (QtCreator). First contains a PRO file for my console application and another project which contains a PRO file for my static library.
    Now I want my application to use the library. To do so, I have updated my app's PRO file (INCLUDEPATH, LIBS, DEPENDPATH) and it seems all is working fine here.

    And now the problem: when I modify the the library it is not automatically rebuilt.

    I have tried another apprach where I have a SINGLE project with 2 PRO files (actually 3, root one is using SUBDIRS to include the other ones), one for executable and one for static library. Executable has LIBS, INCLUDEPATH updated to use the static library. In this case, when I modify the library IT IS compiled but NOT LINKED :/ So the executable is using always the old library.

    What am I doing wrong here ? Is there any special way it needs to be handled in QMake ?

    Thanks in advance!

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #2

    Have you set the dependency in QtCreator? You need to tell creator that your project depends on the library, so it can build the library before the project, and thus link the new code.
    It's in the project configuration pane - dependencies.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    2
    • E Offline
      E Offline
      enmaniac
      wrote on last edited by enmaniac
      #3

      Ah thank you @kshegunov!
      That works perfectly fine for the case number 1 (two saparate projects).

      Is there any way to implement the correct behavior for the case number 2 where SUBDIRS are used ?

      Cheers!

      kshegunovK 1 Reply Last reply
      0
      • E enmaniac

        Ah thank you @kshegunov!
        That works perfectly fine for the case number 1 (two saparate projects).

        Is there any way to implement the correct behavior for the case number 2 where SUBDIRS are used ?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #4

        @enmaniac said in Linking static library to an executable:

        If you use the subdirs template then you can set the dependency between the targets manually in the project file. E.g (from a project of mine):

        TEMPLATE = subdirs
        SUBDIRS += aed_core aed_console
        
        aed_core.subdir = aed-core
        
        aed_console.subdir = aed-console
        aed_console.depends = aed_core           # we depend on the aed_core target
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • E Offline
          E Offline
          enmaniac
          wrote on last edited by
          #5

          Hmm it does not seem to work for me. Here are my PRO files, maybe I am doing something wrong:

          Top level one:

          TEMPLATE = subdirs
          SUBDIRS += myapp staticlib
          
          myapp.subdir = myapp
          staticlib.subdir = staticlib
          
          myapp.depends = staticlib
          

          MYAPP:

          QT += core
          QT -= gui
          
          CONFIG += c++11
          
          TARGET = myapp
          CONFIG += console
          CONFIG -= app_bundle
          
          TEMPLATE = app
          
          SOURCES += main.cpp
          
          # The following define makes your compiler emit warnings if you use
          # any feature of Qt which as been marked deprecated (the exact warnings
          # depend on your compiler). Please consult the documentation of the
          # deprecated API in order to know how to port your code away from it.
          DEFINES += QT_DEPRECATED_WARNINGS
          
          # You can also make your code fail to compile if you use deprecated APIs.
          # In order to do so, uncomment the following line.
          # You can also select to disable deprecated APIs only up to a certain version of Qt.
          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
          
          LIBS += -L$$PWD/../staticlib/debug -lstaticlib
          INCLUDEPATH += $$PWD/../staticlib
          
          

          STATICLLIB:

          QT       -= gui
          
          TARGET = StaticLib
          TEMPLATE = lib
          CONFIG += staticlib
          
          # The following define makes your compiler emit warnings if you use
          # any feature of Qt which as been marked as deprecated (the exact warnings
          # depend on your compiler). Please consult the documentation of the
          # deprecated API in order to know how to port your code away from it.
          DEFINES += QT_DEPRECATED_WARNINGS
          
          # You can also make your code fail to compile if you use deprecated APIs.
          # In order to do so, uncomment the following line.
          # You can also select to disable deprecated APIs only up to a certain version of Qt.
          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
          
          SOURCES += staticlib.cpp
          
          HEADERS += staticlib.h
          unix {
              target.path = /usr/lib
              INSTALLS += target
          }
          

          Again, I can see that staticlib is rebuilt but it is not relinked with the executable :(

          Cheers!

          kshegunovK 1 Reply Last reply
          0
          • E enmaniac

            Hmm it does not seem to work for me. Here are my PRO files, maybe I am doing something wrong:

            Top level one:

            TEMPLATE = subdirs
            SUBDIRS += myapp staticlib
            
            myapp.subdir = myapp
            staticlib.subdir = staticlib
            
            myapp.depends = staticlib
            

            MYAPP:

            QT += core
            QT -= gui
            
            CONFIG += c++11
            
            TARGET = myapp
            CONFIG += console
            CONFIG -= app_bundle
            
            TEMPLATE = app
            
            SOURCES += main.cpp
            
            # The following define makes your compiler emit warnings if you use
            # any feature of Qt which as been marked deprecated (the exact warnings
            # depend on your compiler). Please consult the documentation of the
            # deprecated API in order to know how to port your code away from it.
            DEFINES += QT_DEPRECATED_WARNINGS
            
            # You can also make your code fail to compile if you use deprecated APIs.
            # In order to do so, uncomment the following line.
            # You can also select to disable deprecated APIs only up to a certain version of Qt.
            #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
            
            LIBS += -L$$PWD/../staticlib/debug -lstaticlib
            INCLUDEPATH += $$PWD/../staticlib
            
            

            STATICLLIB:

            QT       -= gui
            
            TARGET = StaticLib
            TEMPLATE = lib
            CONFIG += staticlib
            
            # The following define makes your compiler emit warnings if you use
            # any feature of Qt which as been marked as deprecated (the exact warnings
            # depend on your compiler). Please consult the documentation of the
            # deprecated API in order to know how to port your code away from it.
            DEFINES += QT_DEPRECATED_WARNINGS
            
            # You can also make your code fail to compile if you use deprecated APIs.
            # In order to do so, uncomment the following line.
            # You can also select to disable deprecated APIs only up to a certain version of Qt.
            #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
            
            SOURCES += staticlib.cpp
            
            HEADERS += staticlib.h
            unix {
                target.path = /usr/lib
                INSTALLS += target
            }
            

            Again, I can see that staticlib is rebuilt but it is not relinked with the executable :(

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #6

            How do you use the subdirs project? Have you loaded it into Qt Creator and compiled from there? If so the subtarget dependency won't work - I don't know why exactly but I assume Creator overrides the build order. Try running qmake on the root project from the command line, and if that works, which I expect it should, you can then import (Import build From ... in the project configuration pane) into Creator.

            I haven't been able to find a way to make Creator recognize the subtarget dependencies as of yet if one uses the build directories specified in its UI. Take the last with a grain of salt, however, as it's been some time since I've actually tested this.

            You could also post on the mailing list to get more information on why and/or if it's possible at all as this is a user forum and we don't get much presence from Qt's developers. Or if you're lucky Tobias Hunger ( @hunger ) will shed some more light, he seems to be lurking around in recent times.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            2
            • E Offline
              E Offline
              enmaniac
              wrote on last edited by enmaniac
              #7

              Thank you for your feedback @kshegunov . I will give it a try in the evening.

              Good idea about asking in the mailing list. Hopefully, some more detailed information will be given though ideally I would like someone to make it work :D Kind of weird one needs to switch back to command line to make things work while there is already nice IDE which should allow just that.

              Cheers again! I'll msg here back once I tried your solution later today!

              PS. Sorry forgot the answer your initial question. Yes I loaded it with QtCreator and build from IDE.

              Cheers!

              1 Reply Last reply
              0
              • E Offline
                E Offline
                enmaniac
                wrote on last edited by
                #8

                Ok. I gave it a try with command line. I have re run qmake on my top PRO file and nmake'd to generate library and executable. That succeeded. Changing the static library and nmake'ing all again only rebuilt the static library :( Ehh...

                Anyways, I have posted a question on mailing list as suggested. Lets see what comes out of it.

                Cheers!

                kshegunovK 1 Reply Last reply
                0
                • E enmaniac

                  Ok. I gave it a try with command line. I have re run qmake on my top PRO file and nmake'd to generate library and executable. That succeeded. Changing the static library and nmake'ing all again only rebuilt the static library :( Ehh...

                  Anyways, I have posted a question on mailing list as suggested. Lets see what comes out of it.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #9

                  Okay.
                  If I recall correctly you also would need PRE_TARGETDEPS in your app project file though. I have missed you're not using it. Try adding that too, should fix your issue:

                  PRE_TARGETDEPS += /path/to/library/staticlib.a
                  

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  3
                  • E Offline
                    E Offline
                    enmaniac
                    wrote on last edited by
                    #10

                    Yes, this was exactly that was suggested in the mail list as well :D

                    Thank you again for all your help!

                    Cheers!

                    kshegunovK 1 Reply Last reply
                    0
                    • E enmaniac

                      Yes, this was exactly that was suggested in the mail list as well :D

                      Thank you again for all your help!

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #11

                      No problem. Good luck with your project!

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved