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: interface/implementation: export keyword?




> ----------
> From: 	Terence J. Wells[SMTP:tjw30@cam.ac.uk]
> Reply To: 	tjw30@cam.ac.uk
> Sent: 	Wednesday, October 28, 1998 3:53 AM
> To: 	Joe Buck
> Cc: 	egcs@cygnus.com
> Subject: 	Re: interface/implementation: export keyword?
> 
> 
> > > I'm trying to sensibly separate template definitions/declarations
> for
> > > a project I'm writing in C++, as I believe (?) it's good practice
> to
> > > separate the interface from the implementation.
> > 
> > The short answer is to put your template declarations in an .h file,
> > and your template definitions in a .cxx (or .cc) file, and until
> > egcs supports export, #include the .cxx file as well as the .h file
> > when you use the templates.
> 
> Thanks for the advice, but there's two reasons I didn't do this in the
> first place:
> 
> The first reason is that I couldn't get make to do proper dependency
> checking without rewriting all the rules :-(
> 
SOURCES := $(shell find . -name '*.C')
OBJECTS := $(SOURCES:.C=.o) $(wildcard repo/*.o)
DEPS    := $(SOURCES:.C=.d)

%.d: %.C
	$(SHELL) -ec '$(CXX) -MM $(INCLUDE) $< | sed
'\''s%$(*F).o%$(*D)/& $@%g'\'' > $@'



include $(DEPS)



> Also, I have a template Matrix class (say) which is used by two other
> classes: they both use a Matrix<double>.  If I put the classes in
> separate .cc files I get a bigger executable than if I put them in one
> big .cc file, which (I imagine) means that my final executable has two
> copies of the Matrix<double> code instead of one -- I'm not familiar
> with the structure of the object code so I can't be sure.  Is there
> any way of getting egcs (or the linker) to eliminate redundant
> instantiations? 
You will get bigger object files, but the final executable will have
only one copy of each template function that is used.  To do this egcs
uses 'weak' symbols for which the linker discard duplicates.

> I believe that this can be solved by explicitly instantiating the
> needed classes in the template .cc file and linking against the
> resulting .o file, but this requires some clever dependency checking
> and preprocessor magic beyond my current skills.
Is there really anything to solve?  Just include all template
definitions and let g++/linker deal with the rest.

> How does the STL do it?  Does the compiler have to emit code for all
> the template instantiations itself and put them in executables, or is
> there some magic way libraries use to handle arbitrary types for
> template instantiations?
When optimization is on the compiler only emits non-inline template
functions that are actually used in each translation unit.  

> Surely there must be an elegant way of doing this already, or does
> nobody else use templates and require them to be space-efficient?
I would guess that very few people don't use templates these days.  The
standard is full of them, even istream and ostream are implemented with
templates.

Just try what Joe said, if that doesn't work then you either forgot to
include a .cc file, your you've found a bug.  Also make sure the .cc
files you include from headers don't have any non-template definitions
or you will get multiple definition errors.

> Thanks,
> 
> TJW
> 
> --------------------------------------------------------------------
> Terence J. Wells                E-mail  : tjw30@cam.ac.uk
> St John's College               Phone   : +44.468.916617 (mobile)
> Cambridge CB2 1TP               Room    : A3 New Court, SJC
> United Kingdom                  Talk    : tjw30@tjw30.joh.cam.ac.uk
> 


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