From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,88a75744c73ebcc8,start X-Google-Attributes: gidfee84,public From: Marcin Kasperski Subject: Q: CXX Template instantiation mechanisms... Date: 1998/04/24 Message-ID: <354085FC.6D212CA1@crt.tpsa.pl>#1/1 X-Deja-AN: 347279372 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=iso-8859-2 Organization: http://news.icm.edu.pl/ Mime-Version: 1.0 Newsgroups: comp.unix.osf.osf1 Digital CXX has fairly nice template instantiation mechanism. In a few words, it put all needed template instantiations into subdir cxx_repository, and reuses them when needed. But sometimes it refuses to make global instantiation and instead of it add instantiation into code of module which needed it. Then, if two different modules uses this instantiation, during linking I get name conflict. The situation is described in a piece of Digital CXX documentation which I'd like to quote: > In some cases, DIGITAL C++ is unable to write template instantiations into the repository. When a template instantiation references a variable or function that is local to the source file, that variable or function is generated as part of the user's object file instead of in the repository. It is an error to link an application that has differing instantiations of the same template, which would be the case if a template that referenced local variables were instantiated in two or more different source files. An attempt to do so is diagnosed at link time with a multiply defined error. The description above is rather short... I found myself to achieve such an error after every greater code change. I located, that problem can be for instance caused by static variable placed in header file, exactly it happens when we have: ---- header.h ... static char* = "XXX"; ... --- templ.h template void swap (X& a, X& b); --- templ.cc template void swap (X& a, X& b) { X tmp = a; a = b; b = tmp; } --- file1.cc #include "header.h" #include "templ.h" ... int a,b; swap (a,b); --- file2.cc #include "header.h" #include "templ.h" ... int a,b; swap (a,b); ---- cxx file1.cc file2.cc plik2.o: swap(int,int) multiply defined This example is real, some library I use places in headers lines like static char h_...[]="@(#)..."; I found also, that problem happens when header needed to implement template is not included to template implementation file but into file which instantiates template. But tracing that is hard. I'd be grateful for: - any other scenarios which could cause such a behaviour - any advice/link to tool/idea of application/... of how to atomatically trace such a problems (i.e get to know why this and this template is being instantiated locally) - any more or less general remarks about the situation Marcin Kasperski marcink@crt.tpsa.pl PS. Please, CC answers to me... I use mail internet/intranet gateway. I haven't such for news...