This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Suggested FAQ entry



>  
> +<h2><a name="vtables">When building C++, the linker says my constructors, destructors or virtual tables are undefined, but I defined them</a></h2>
> +
> +<p>The C++ Standard specifies that all virtual methods of a class that
> +are not pure-virtual must be defined, but does not require any
> +diagnostic for violations of this rule [class.virtual]/8.  Based on
> +this assumption, egcs will only emit the implicitly defined
> +constructors, the assignment operator, the destructor and the virtual
> +table of a class in the translation unit that defines its first such
> +non-inline method.
> +
> +<p>Therefore, if you fail to define this particular method, the linker
> +may complain about the lack of definitions for apparently unrelated
> +symbols.  Unfortunately, in order to improve this error message, it
> +might be necessary to change the linker, and this can't always be
> +done.
> +
> +<p>The solution is to ensure that all virtual methods that are not
> +pure are defined.  Note that a destructor must be defined even if it
> +is declared pure-virtual [class.dtor]/7.
> +
> +<hr>

Can I add to this discussion a gcc-specific point, which is that if you're
using #pragma interface, and forget or mismatch the #pragma implementation
You will get the same problem.  For example, the pair of files below result 
in an undefined "pragma_imp virtual table"

*** /tmp/emptu/pragma-imp.cc    Thu Jan  1 01:00:00 1970
--- pragma-imp/pragma-imp.cc    Tue Apr 13 16:23:42 1999
***************
*** 0 ****
--- 1,7 ----
+ 
+ #include "pragma-imp.h"
+ 
+ int main(int, char **)
+ {
+   pragma_imp p;
+ }
diff -Pc /tmp/emptu/pragma-imp.h pragma-imp/pragma-imp.h
*** /tmp/emptu/pragma-imp.h     Thu Jan  1 01:00:00 1970
--- pragma-imp/pragma-imp.h     Tue Apr 13 16:23:00 1999
***************
*** 0 ****
--- 1,5 ----
+ #pragma interface
+ 
+ struct pragma_imp {
+   virtual void f();
+ };


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]