Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Shared code in subdirs template?
Qt 6.11 is out! See what's new in the release blog

Shared code in subdirs template?

Scheduled Pinned Locked Moved Qt Creator and other tools
12 Posts 3 Posters 6.9k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      Generate a static library instead.

      "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

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

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

          "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

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Ah-oh. Awkward. Thanks.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              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 to SayaMap::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.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                Franzk
                wrote on last edited by
                #7

                Something like
                @
                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.

                "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

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    Franzk
                    wrote on last edited by
                    #9

                    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.

                    "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

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        So it will be different on Windows?

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

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

                          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