Linking static library to an executable
-
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. -
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 ?
-
@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
-
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 :(
-
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.
-
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.
-
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.
-
Okay.
If I recall correctly you also would needPRE_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