October 19, 2011

wecing wecing
Lab Rat
22 posts

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.

11 replies

October 19, 2011

Franzk Franzk
Lab Rat
830 posts

Generate a static library instead.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

October 19, 2011

wecing wecing
Lab Rat
22 posts

Er… I mean I don’t want to use .so or .dll files.

October 19, 2011

Franzk Franzk
Lab Rat
830 posts

Static libraries [en.wikipedia.org] are libraries that are linked into your program. You won’t be needing a dll or so in that case.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

October 19, 2011

wecing wecing
Lab Rat
22 posts

Ah-oh. Awkward. Thanks.

October 20, 2011

wecing wecing
Lab Rat
22 posts

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+0×730): undefined reference to `SayaMap::SayaMap(QString)’
Saye.cpp:(.text+0×7b8): 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.

October 20, 2011

Franzk Franzk
Lab Rat
830 posts

Something like

  1. LIBS+=-L/path/to/common/libs -lcommon

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.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

October 20, 2011

wecing wecing
Lab Rat
22 posts

Nmmm… In fact I tried

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

October 20, 2011

Franzk Franzk
Lab Rat
830 posts

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.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

October 20, 2011

Volker Volker
Robot Herder
5428 posts

This worked for me on unix/linux:

  1. PRE_TARGETDEPS = ../common/libcommon.a

This ensures that the final target is rebuilt, once libcommon.a has changed.

October 20, 2011

wecing wecing
Lab Rat
22 posts

So it will be different on Windows?

October 20, 2011

Volker Volker
Robot Herder
5428 posts
wecing wrote:
So it will be different on Windows?

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

 
  ‹‹ qt addin bug (VS2008)      Qt Creator: Widgets are not added to h file ››

You must log in to post a reply. Not a member yet? Register here!