PDA

View Full Version : Templates, GCC and Qt Creator



Zingam
18th April 2011, 07:25
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:


template<class T>
class StackBase
{
public:
StackBase(){};
virtual void push(const T& x) = 0;

Then I have this:

template<class T>
class StackArray : public StackBase<T>
{
public:
StackArray(){std::cout << "OK! Constructed!\n";};
void push(const T& x){};

...and now I use it:

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

StackArray<char> sac;
sac.pop();


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

SixDegrees
18th April 2011, 08:00
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.

Zingam
18th April 2011, 08:45
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?

SixDegrees
18th April 2011, 10:07
If your build isn't detecting changes to your source or header files, there's likely something wrong with your project file.

Zingam
18th April 2011, 17:08
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

wysota
18th April 2011, 20:42
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.

Zingam
18th April 2011, 23:14
Thank you! I think I've got it to work now.