Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Howto share STL code between modules

4 views
Skip to first unread message

Tom

unread,
Jun 21, 2003, 12:41:09 AM6/21/03
to
I have an application that consists of several seperately compiled & linked modules. (Actually, they're DLL's and I'm using VC7.1.) Each module is compiled from C++ code that uses the same STL code. Naturally, I would rather only have one copy of the code.

Now, I realize that no object code is created until a template is instantiated, but on the other hand, I assume that if I have 5 different instantiations of a std::vector of 4 bytes values (eg. pointers, references, int's, long's, etc.) in a single module the compiler/linker is NOT going to produce 5 copies of the vector code, right (assuming that the compiler is half decent). So, by the same token, why shouldn't I be able to share my vector code between multiple modules, or even multiple processes?

What about static link libraries? I'm guessing that a static library is just a bunch of unlinked object files, so the linker could still perform optimizations when it puts the final exe together. Is the situation the same with Linux and GCC?

Thanks, Tom.

Pete Becker

unread,
Jun 21, 2003, 12:59:01 PM6/21/03
to
Tom wrote:
>
> I have an application that consists of several seperately compiled & linked modules. (Actually, they're DLL's and I'm using VC7.1.) Each module is compiled from C++ code that uses the same STL code. Naturally, I would rather only have one copy of the code.
>
> Now, I realize that no object code is created until a template is instantiated, but on the other hand, I assume that if I have 5 different instantiations of a std::vector of 4 bytes values (eg. pointers, references, int's, long's, etc.) in a single module the compiler/linker is NOT going to produce 5 copies of the vector code, right (assuming that the compiler is half decent). So, by the same token, why shouldn't I be able to share my vector code between multiple modules, or even multiple processes?
>

To do this you need two things: you need to be able to say "don't
instantiate this template instance here", and you need to be able to say
"instantiate this template instance here." The latter is part of the
language. The former isn't, but it's under discussion. Several compilers
support

extern template class foo<int>;

and that's likely to be the approved syntax. So what you do, if your
compiler supports it (and VC++ does), is this:

// client code
extern template class foo<int>; // dont instantiate here, but okay to
use it

// template library
template class foo<int>; // instantiate here

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Tom

unread,
Jun 21, 2003, 2:39:34 PM6/21/03
to

"Pete Becker" <peteb...@acm.org> wrote in message news:3EF48ED5...@acm.org...
> Tom wrote:...

> To do this you need two things: you need to be able to say "don't
> instantiate this template instance here", and you need to be able to say
> "instantiate this template instance here." The latter is part of the
> language. The former isn't, but it's under discussion. Several compilers
> support
>
> extern template class foo<int>;
>
> and that's likely to be the approved syntax. So what you do, if your
> compiler supports it (and VC++ does), is this:
>
> // client code
> extern template class foo<int>; // dont instantiate here, but okay to
> use it
>
> // template library
> template class foo<int>; // instantiate here

Great. Thanks.

So, if I understand correctly, I could create a DLL that exported basic 32bit instantiations (eg. vector< void* >) of the STL containers, and then all my client modes could include code like:

extern template class foo< int > ; any 32bit type

and the linker would link the client modules to the appropriate code in the DLL. It's a little ackward, but it's good to know that (and how) I can do it with VC7.1. I'm very pleased with the VC7.1 compiler and standard library (good work). It's just too bad that the IDE sucks. Maybe it's time for me to look at something like Visual SlickEdit.

Thanks, Tom.

Pete Becker

unread,
Jun 21, 2003, 2:52:27 PM6/21/03
to
Tom wrote:
>
> So, if I understand correctly, I could create a DLL that exported basic 32bit instantiations (eg. vector< void* >) of the STL containers, and then all my client modes could include code like:
>
> extern template class foo< int > ; any 32bit type
>
> and the linker would link the client modules to the appropriate code in the DLL.

Well, yes, if you aren't looking for anything special by calling out 32
bit types. In particular, vector<void*> and vector<int> are two
different types, even though both hold 32 bit types.

0 new messages