Re: Header file + implementation file with template
From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 04/30/04
- Next message: B. v Ingen Schenau: "Re: long veriable causes problem"
- Previous message: B. v Ingen Schenau: "Re: C++ Conversion functions for pointers"
- In reply to: cpp: "Header file + implementation file with template"
- Next in thread: Kevin Chen: "Re: Header file + implementation file with template"
- Reply: Kevin Chen: "Re: Header file + implementation file with template"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 30 Apr 2004 11:31:17 +0200
cpp wrote:
> Hi folks,
> I have a problem on using header file and template.
>
> If I put the interface/declaration of a class template in a header
> file (foo.h) and put the implemenation in to a cpp file (foo.cpp).
>
> When I compile it using gcc 3.3.2 (cygwin) and 2.5.0(redhat):
>
> g++ -c foo.cpp (Ok here, generate foo.o)
> g++ -o test test.cpp foo.o (linking error here)
> Error message:
> /cygdrive/d/code/ccqie7hk.o(.text+0x25):test.cpp: undefined reference
> to 'Foo<int>::bar()' collect2: ld returned 1 exit status
>
> If I move the implementation code in foo.cpp to foo.h and leave only
> #include "foo.h" statement there, the linker won't complain.
>
> My question is, do I have to put all the implementation detail in a
> the header file?
Until a sufficiently large number of compilers start supporting the export
keyword, the answer is Yes.
> I searched and found
>
http://www.flipcode.com/cgi-bin/msg.cgi?showThread=00000905&forum=general&id=-1
> Is it a common practice to #include a .inl file in a header? It seems
> weird to me.
For templates that is common practice.
But keep in mind that the extension .inl is not magical. People just as
often use .tcc or even .cpp.
The idea behind this is that you still have the logical separation of
interface and implementation, even if the compiler does not support a
physical separation.
>
> Thanks.
>
Bart v Ingen Schenau
-- a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
- Next message: B. v Ingen Schenau: "Re: long veriable causes problem"
- Previous message: B. v Ingen Schenau: "Re: C++ Conversion functions for pointers"
- In reply to: cpp: "Header file + implementation file with template"
- Next in thread: Kevin Chen: "Re: Header file + implementation file with template"
- Reply: Kevin Chen: "Re: Header file + implementation file with template"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|