Results 1 to 7 of 7

Thread: Templates, GCC and Qt Creator

  1. #1
    Join Date
    Mar 2011
    Posts
    29
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Templates, GCC and Qt Creator

    Hi, I'm trying to learn more about templates but stumbled upon something I do not understand.
    I have declared an abstract class that starts like that:

    Qt Code:
    1. template<class T>
    2. class StackBase
    3. {
    4. public:
    5. StackBase(){};
    6. virtual void push(const T& x) = 0;
    7.  
    8. Then I have this:
    9.  
    10. template<class T>
    11. class StackArray : public StackBase<T>
    12. {
    13. public:
    14. StackArray(){std::cout << "OK! Constructed!\n";};
    15. void push(const T& x){};
    16.  
    17. ...and now I use it:
    18.  
    19. int main(int argc, char *argv[])
    20. {
    21. QCoreApplication a(argc, argv);
    22.  
    23. StackArray<char> sac;
    24. sac.pop();
    To copy to clipboard, switch view to plain text mode 
    (Plz. note that's not a finished class at all).

    Then I've found out that if I change any of the templated classes (if I change any of the files) and if I just "Build" all I get is a linker error of unspecified StackArray();
    I have to do "Rebuild" (or "Clean" and "Build") to make it compile and link.

    Am I doing something wrong here? Is that how it is supposed to be? Does it mean that I have to do a full rebuild everytime I'm working with templates even if it is a large project that takes an hour to compile.
    Last edited by high_flyer; 18th April 2011 at 17:17. Reason: code tags

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

    Default Re: Templates, GCC and Qt Creator

    Templates are not code - they're templates that the compiler uses to generate code once their type is resolved. As such, any change to template source files requires another pass through the compiler, then more passes are required by anything downstream in the build that uses those classes.

    Some/most compilers support precompilation of headers which can cut down on some of this, but in general a change to a template file is going to require at least some rebuilding. It's similar to what happens when any header file is touched; any file that includes it will require another pass through the compiler.

  3. #3
    Join Date
    Mar 2011
    Posts
    29
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates, GCC and Qt Creator

    So what does this basically mean? All templated code should be compiled in separate libraries and tested before it is included in a larger project and I always need to use "Rebuild" in that case because the compiler cannot do it on its own?
    Why doesn't the compiler detect that the file with template definitions has changed and recompile it automatically?
    Last edited by Zingam; 18th April 2011 at 09:43.

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

    Default Re: Templates, GCC and Qt Creator

    If your build isn't detecting changes to your source or header files, there's likely something wrong with your project file.

  5. #5
    Join Date
    Mar 2011
    Posts
    29
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates, GCC and Qt Creator

    I use Qt creator and I haven't done anything fancy I think. Here is my project file. I don't see anything abnormal in there.

    #-------------------------------------------------
    #
    # Project created by QtCreator 2011-04-11T11:04:57
    #
    #-------------------------------------------------

    QT += core

    QT -= gui

    TARGET = Stack-Test
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app


    SOURCES += main.cpp \
    stack.cpp \
    stackarray.cpp \
    stacklinkedlist.cpp \
    stackdynamicarray.cpp

    HEADERS += \
    stack.h \
    stackarray.h \
    stacklinkedlist.h \
    stackdynamicarray.h \
    stackbase.h

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Templates, GCC and Qt Creator

    For gcc to detect a dependency on a file, the template needs to be present in a .h file that is later explicitly included in some .cpp file. Then the compiler knows this cpp file depends on the header file and whenever the header file changes, the cpp file will be recompiled.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2011
    Posts
    29
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Templates, GCC and Qt Creator

    Thank you! I think I've got it to work now.

Similar Threads

  1. help with templates please
    By GrahamLabdon in forum Newbie
    Replies: 2
    Last Post: 4th February 2011, 17:29
  2. File templates in Qt Creator
    By JovianGhost in forum Qt Tools
    Replies: 5
    Last Post: 15th March 2010, 01:01
  3. Qt Creator + file templates
    By NoRulez in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2009, 22:57
  4. templates
    By mickey in forum General Programming
    Replies: 5
    Last Post: 16th January 2008, 15:25
  5. Templates : Help Please
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 14th February 2006, 13:50

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
  •  
Qt is a trademark of The Qt Company.