Nominate our 2022 Qt Champions!

Right way to create a library (QTProject) and use this in another project

  • Hi!

    How is the right way to create a library project and reference this project in other projects in QT.

    I did a reference using INCLUDEPATH but I'm getting 'undefined reference' error on the objects.


  • hi @GustavoCaldeira, have you tried adding:

    LIBS += -L"libpath" -llibname

    (pay attention to the naming convention of the library file - it must begin with prefix "lib")
    (also, the double "l" in the last parameter is not a typo, but it comes from the parameter starting with "-l")

    in your .pro file?

  • @ucmar thanks by the reply!

    Well, I inserted this way:

    LIBS += -L"../infinity-commons" -llibInfinityCommons

    ../infinity-commons is the folder of the other project...

    and now, how do I use a class that is inside the library? I included the .h file but it was not found...

  • @GustavoCaldeira regarding lib file convention, you should rather be writing

    LIBS += -L"../infinity-commons" -lInfinityCommons

    and your file should be named libInfinityCommons.dll
    (in practice, you have to omit the prefix "lib" in the .pro file, but the file name must have it)

    If your class header is not found it means there is some problem with your INCLUDEPATH. Try using an absolute path first (i.e. C:...) and then, if the header is found, try to change it to a relative path.

    If the header gets found, while you are typing the #include directive the autocompletion will list your header filename.

    Another hint: consider that the path in the LIBS += ... directive must point to the folder where the binary library file is located, which, in general, is not the same folder as the library project source files folder! For example, you can have a shadow-build directory located elsewhere...

  • Thanks @ucmar

    This worked for me... my .pro is now like this:

    LIBS += -L"/myhome/build-folder/infinity-commons" -lInfinityCommons
    INCLUDEPATH += ../infinity-commons

  • @GustavoCaldeira you are welcome, glad it worked. Could you please mark the thread as solved? Thanks!