Qt Creator: linking libs from multiple projects


  • Moderators

    I'm a long time user of Visual Studio trying to switch to Qt Creator.
    I have a project(solution in VS) that consists of several sub-projects. Few libraries that depend on each other and a main app (exe).

    To make it simple lets consider the following file structure. This is the default layout QtCreator creates for 2 projects :
    @AppMain
    appmain.pro
    main.cpp
    AppMain-build
    debug
    AppMain.exe
    release
    AppMain.exe
    Makefile etc.

    AppLib1
    applib1.pro
    applib1.h
    applib1.cpp
    AppLib1-build
    debug
    applib1.a
    release
    applib1.a
    Makefile etc.@

    How do I link form the AppMain to this AppLib1?
    In the main.cpp I've included .h file like so:
    @#include "../AppLib1/applib1.h"@
    The compilation fails obviously because of the missing dependencies. How do I add this?

    In VS this was done via "Project dependencies" dialog. I found something that looks similar in QtCreator in the "Projects" pane, "Dependencies" tab, but selecting AppLib1 from the list doesn't change anything. Linker is still showing undefined references from the lib.

    There is also option to add a library in the appmain.pro file editor from the context menu by selecting "Add library...", but it only allows me to select .lib files, which I don't have since I'm using minGW (library is in .a file).

    Can I add it somehow manually by LIBS += in the .pro file? I need it to switch between the debug and release applib1.a file, and the paths can't be absolute because this project is in version control used on several machines.

    I'm using RC of Qt Creator 2.1 with minGW on Windows7 x64

    Any help will be great.



  • You should add manually to the .pro file something like this:
    @
    debug:LIBS+=debug_library_file_name
    release:LIBS+=release_library_file_name
    @

    Also you should set "Dependencies" in the "Projects" pane.

    You may use session (menu File - Session) to save the settings and load them on next run.

    I am not sure what exactly "library_file_name" is on Windows, probably it is "library_file_name.a"

    Path in library_file_name may be relative.

    Also see this post: https://developer.qt.nokia.com/forums/viewthread/1913


  • Moderators

    I ended up with something like this:
    @debug: LIBS += -L"../AppLib1-build/debug"
    release: LIBS += -L"../AppLib1-build/release"
    LIBS += -lApplib1
    @
    Wow, I can see now that the transition from VS will be super-confusing for me (the project is quite large), all this manual "configuration" work..

    Anyway, thanks a lot blex.
    One question though - what exactly does checking projects in "Dependencies" tab do? It doesn't seem to matter if it's checked or not in my case.



  • [quote author="crossblades" date="1290890172"]
    One question though - what exactly does checking projects in "Dependencies" tab do? It doesn't seem to matter if it's checked or not in my case.[/quote]

    I think it is build order



  • [quote author="crossblades" date="1290890172"]I ended up with something like this:
    @debug: LIBS += -L"../AppLib1-build/debug"
    release: LIBS += -L"../AppLib1-build/release"
    LIBS += -lApplib1
    @
    [/quote]

    You also can add this to shorten the includes:
    @
    INCLUDEPATH += ../Applib1
    @

    Then this include works:
    @
    #include "applib1.h"
    @

    [quote author="crossblades" date="1290890172"]I ended up One question though - what exactly does checking projects in "Dependencies" tab do? It doesn't seem to matter if it's checked or not in my case.[/quote]

    If you set it up, the build order is calculated correctly and if you modify the lib the app is relinked afterwards to pick up the changes. If you don't you might end up with an old application that is not linked against the new build of your lib.


  • Moderators

    I would suggest turning both library and application into one project by adding another .pro-file using the SUBDIRS template. That is a way to wrap many .pro-files into one project.

    Using this combined project it is way more straight forward to set library and include pathes.

    If you only rarely work on the library and do not want to have it open in creator all the time then you might want to consider to properly install the library in your system... that makes working with it way easier, too.


  • Moderators

    Thanks for all replies.
    My whole project consists of several "base" libraries and a few exe projects - something similar to Qt - some base libs are held in QtCore, QtGui etc and bunch of executables share this base (Creator, Designer, Linguist..), except they're statically linked by default.

    I'll look into the suggested SUBDIRS template. From Your description this looks like something that might be useful to me.
    Btw are there any good docs to the .pro files format? I can't seem to find any "oficial" spec, so I'm learning by going through Qt srcs, but it's hard if You don't exactly know what You're looking.

    At this beginning stage all of the components are changing/growing often. I usually have all or almost all of them opened at the same time.
    What do you mean by properly installing the libs? I don't think there is any standard way to "install" a library(.a or .lib) in windows, except for maybe adding it to the environment path, but I don't need that.



  • You can find the complete documentation "here":http://doc.qt.nokia.com/4.7/qmake-manual.html. The same content is provided in Qt Assistant, btw.

    I think Tobias meant "installing" in the sense of installing it (a DLL probably) into some system directory. But from what you've writte this seems not to be the solution for you.


  • Moderators

    Thanks! I'm really scratching my head how could I miss that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.