Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Shared code in subdirs template?
Currently I'm making a project which will finally generate two executables, (and of course) with qmake.
They share some code, so I put the shared part to a directory called "common", just under the root directory.
Code for these two executables are also put into separate directories under the root dir; one called "game", the other one called "editor".
I don't know how to use code in the "common" directory; I want to compile the shared code directly into these executables, so setting its template to "lib" and generate shared objects may not be a good solution for me.
Generate a static library instead.
Er... I mean I don't want to use .so or .dll files.
"Static libraries":http://en.wikipedia.org/wiki/Static_library are libraries that are linked into your program. You won't be needing a dll or so in that case.
Ah-oh. Awkward. Thanks.
Well, I successfully generated libcommon.a in common/; but I don't know how to make the final executables use it.
I tried to insert these lines into the .pri files, but it seems not working.
DEPENDPATH += ../common/
INCLUDEPATH += ../
POST_TARGETDEPS += ../common/libcommon.a
Here is the error log:
g++ -Wl,-O1 -Wl,-rpath,/usr/lib64/qt4 -o ../saye build/main.o build/Saye.o build/GameWindow.o build/SayeMainWindow.o build/moc_Saye.o build/moc_GameWindow.o build/moc_SayeMainWindow.o -L/usr/lib64/qt4 -L/usr/X11R6/lib -lSimpleAV_SDL -lSimpleAV
sdl-config --libs-lSDL_mixer -L/usr/local/lib -pthread -lavformat -lavcodec -ldl -lasound -lbz2 -lz -lswscale -lavutil -lm -lQtXml -L/usr/lib64 -L/usr/lib64/qt4 -lQtOpenGL -L/usr/X11R6/lib -lQtGui -lQtCore -lgthread-2.0 -lrt -lglib-2.0 -lGLU -lGL -lpthread
build/Saye.o: In function
Saye::openProject()': Saye.cpp:(.text+0x730): undefined reference toSayaMap::SayaMap(QString)'
Saye.cpp:(.text+0x7b8): undefined reference to `SayaMap::dump()'
collect2: ld returned 1 exit status
make: *** [../saye] Error 1
The SayaMap class is defined in common/. Almost everything works well; this issue happens when linking the object files.
Adding the path to common libs is only necessary if they're not already in your search path. You could try with -lcommon first to see if it has the desired effect already.
Nmmm... In fact I tried
@LIBS += ../common/libcommon.a@
Very ugly, but works.
But here comes another question: what if we later updated common? Code used it should be recompiled, but make will not think it needs to do so.
Maybe I should switch back to shared libraries.
I'm not sure how to handle it directly with qmake, but you can always touch a header file (Saye.h for example) to force a rebuild & relink. Of course just a relink only would be nicest.
goetz last edited by
This worked for me on unix/linux:
PRE_TARGETDEPS = ../common/libcommon.a
This ensures that the final target is rebuilt, once libcommon.a has changed.
So it will be different on Windows?
goetz last edited by
[quote author="wecing" date="1319138207"]So it will be different on Windows?[/quote]
For MinGW this should work too, as it's a gnu toolchain. It should work for the visual studio toolchain together with Qt Creator as IDE too.
I don't know if it works with full Visual Studio usage (including the IDE/GUI).