Results 1 to 12 of 12

Thread: Including a self-made library into another project

  1. #1
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Including a self-made library into another project

    Hi.
    I'm trying to create a program with one library and one executable that invokes that library.

    At first, I've created one folder for the application, and then three subfolders, one for a test executable project, one for the library project and one that's the path in which both library and executable are copied.

    APP
    |
    |- bin
    |- MyLibrary
    | |- MyLibrary <== .pro and sources
    | |- MyLibrary-build-desktop
    |- TestApp
    | |- TestApp <== .pro and sources
    | |- TestApp-build-desktop


    This is the .pro of the library

    Qt Code:
    1. QT -= gui
    2. TARGET = MyLibrary
    3. TEMPLATE = lib
    4. DEFINES += MYLIBRARY_LIBRARY
    5.  
    6. SOURCES += mylibrary.cpp
    7. HEADERS += mylibrary.h\
    8. MyLibrary_global.h \
    9.  
    10. # Copying to destination folder
    11. DESTDIR = ../../bin
    To copy to clipboard, switch view to plain text mode 

    This one is the .pro of the test application
    Qt Code:
    1. QT += core
    2. QT -= gui
    3. TARGET = TestApp
    4. CONFIG += console
    5. CONFIG -= app_bundle
    6.  
    7. TEMPLATE = app
    8. SOURCES += main.cpp
    9. # MeteoGrid Library
    10. LIBS += -L../../bin/libMyLibrary.so
    11. INCLUDEPATH += ../../MyLibrary/MyLibrary
    12.  
    13.  
    14. # Copying to destination folder
    15. DESTDIR = ../../bin
    To copy to clipboard, switch view to plain text mode 

    When I compile the library, it's successfully copied into the bin directory.

    If I include the mylibrary.h header into the test application, it finds it and it compiles.
    But, if I try to use the class defined in my library

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2.  
    3. #include "mylibrary.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. MyLibraryClass<qint32> xTest (1,1,1);
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    I obtain the error:

    main.cpp.text+0x39): undefined reference to `MyLibraryClass<int>::MyLibraryClass(int, int, int)'
    I'd like to know where's the error, and how can I compile successfully the executable.

    Thanks in advance for your replies.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Including a self-made library into another project

    it looks like MyLibraryClass<T> does not have a constructor with the signature MyLibraryClass(T,T,T) , or that its header is not included.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Including a self-made library into another project

    It's defined...

    This is the declaration in my .h file

    Qt Code:
    1. //! Constructor that defines only grid dimension
    2. MyLibrary(qint32 qiX, qint32 qiY, qint32 qiZ);
    To copy to clipboard, switch view to plain text mode 

    This is the implementation in my .cpp file

    Qt Code:
    1. template<class T>
    2. MyLibrary<T>::MyLibrary(qint32 qiX, qint32 qiY, qint32 qiZ)
    3. {
    4.  
    5. // Testing values to be greater or equal to zero.
    6. if (qiX < 0)
    7. {
    8. qDebug() << "MyLibrary<T>::MyLibrary: qiX = " << " is less than zero."
    9. << " Setting X value to zero.";
    10. m_qiX = 0;
    11.  
    12. }
    13. else
    14. {
    15. m_qiX = qiX;
    16. }
    17.  
    18. // etc etc etc...
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Including a self-made library into another project

    You are mixing specialization and general template.
    For specializazoin you need the following syntax:
    Qt Code:
    1. template<>
    2. MyLibrary::MyLibrary<quint32>(qint32 qiX, qint32 qiY, qint32 qiZ)
    3. {...
    4. }
    To copy to clipboard, switch view to plain text mode 
    Or a general template:
    Qt Code:
    1. template<class T>
    2. MyLibrary::MyLibrary(T qiX, T qiY, T qiZ)
    3. {...
    4. }
    To copy to clipboard, switch view to plain text mode 

    Ah and:
    This is the implementation in my .cpp file
    templates are implemented in headers, not in *.cpp files.
    Or more correctly said:
    The implementation (definition) of a template class or function must be in the same file as its declaration, and this is usually done in a header.
    Read more abut how to create and use templates in c++.
    Last edited by high_flyer; 26th May 2011 at 13:58.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Including a self-made library into another project

    I don' use T for arguments of constructor. It defines the type of a private attribute of my class, that's used inside my constructor. qiX,Y,Z are indexes to access to a (T m_xGrid[][][]) structure inside my class.

    Qt Code:
    1. template<class T>
    2. MyLibrary<T>::MyLibrary(qint32 qiX, qint32 qiY, qint32 qiZ)
    3. {
    4.  
    5. // Testing values to be greater or equal to zero.
    6. if (qiX < 0)
    7. {
    8. qDebug() << "MyLibrary<T>::MyLibrary: qiX = " << " is less than zero."
    9. << " Setting X value to zero.";
    10. m_qiX = 0;
    11.  
    12. }
    13. else
    14. {
    15. m_qiX = qiX;
    16. }
    17.  
    18. // etc etc etc...
    19.  
    20. // Example of use of T
    21. T xValue;
    22. xValue = m_xGrid[1][1][1];
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Including a self-made library into another project

    It doesn't matter.
    Your syntax is wrong and your implementation is in the wrong place.
    Read well what I wrote, and google to some template tutorials.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Including a self-made library into another project

    Ok, sorry... I'm reading something more about templates. Thanks for your reply.
    Regards.

  8. #8
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Including a self-made library into another project

    Also, your application project file doesn't look right. 'LIBS += -L../../bin/libMyLibrary.so' just adds the directory to the linker search path; you need to add '-lMyLibrary' to it. The 'undefined reference' error indicates that the linker can't find your library.

    But note that whatever else may be in your library, templated classes will not appear. Templates aren't code, and aren't compiled into libraries; templates are...templates that tell the compiler how to generate code when an actual instantiation of a template class occurs, so unless your library creates an instance of a template class there is nothing in it that's template related. The only place the template is defined is in the header, which is roped in at compile time.

    This is all somewhat oversimplified.
    Last edited by SixDegrees; 27th May 2011 at 07:56.

  9. #9
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Including a self-made library into another project

    Thanks. Just to know, If I can't export a template with a library, how can works Qt container like, for example, QVector? Because in that case I simply include the qvector header and I can use the template. How can I do something similiar?

  10. #10
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Including a self-made library into another project

    You include all of your implementation in your header file so there's nothing to compile and nothing to build into a link library.

  11. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Including a self-made library into another project

    If I can't export a template with a library
    You can deploy a header containing the template definition with other headers in library, I don't see a problem here.
    Or do you mean you want to have a template code in .so file somehow ? In that case I'd suggest you read more about templates.
    If this library is nothing more than a template, then you don't need any .so files, no linking to the library is required, because there is nothing to link to. All needed code will be generated in case of template instantiation.
    So in fact using this library means including the header and creating an instance of template class (or method).

  12. #12
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Including a self-made library into another project

    Thanks, I've also read something about that. It's the first time that I use it for a serious project, so before I'd never noticed template differences from normal classes because I've used them always in the same project...

    I have a lot to learn :-)

    Thanks to anyone.

Similar Threads

  1. Including a static library
    By jepessen in forum Newbie
    Replies: 3
    Last Post: 6th May 2011, 09:35
  2. linux project made in windows
    By alimooghashang in forum Qt Programming
    Replies: 2
    Last Post: 26th August 2010, 06:46
  3. Including GMP in Qt-Project
    By tillorgias in forum Newbie
    Replies: 3
    Last Post: 3rd July 2010, 20:55
  4. Problem including library (dllimport)
    By cae in forum Qt Programming
    Replies: 9
    Last Post: 28th May 2010, 06:53
  5. Replies: 4
    Last Post: 18th December 2009, 18:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.