 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Steven C. Guest
|
Posted: Sat Sep 27, 2003 6:51 pm Post subject: templates & obj |
|
|
When you compile a cpp program that includes a template class in a .h file
where does the compiled code go for that templated class? If it went in the
obj for the cpp program seems to me you could end up with multiple templates
of the same type if the .h file was included in multiple .cpp. Seems more
straight forward that it would be put in a obj for the .h and the compiler
would just add new ones to the obj for template types that had not been
created by other cpp.
Or if it was included in every cpp obj then I guess it would have a flag in
the obj for to ignore duplicates of the same template type.
Just curious how it worked.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Sep 27, 2003 7:36 pm Post subject: Re: templates & obj |
|
|
"Steven C." <nospam (AT) xxx (DOT) com> wrote...
| Quote: | When you compile a cpp program that includes a template class in a .h file
where does the compiled code go for that templated class?
|
Somewhere in the same obj file.
| Quote: | If it went in the
obj for the cpp program seems to me you could end up with multiple
templates
of the same type if the .h file was included in multiple .cpp.
|
Yes, and the linker is supposed to eliminate the multiplicity.
| Quote: | Seems more
straight forward that it would be put in a obj for the .h and the compiler
would just add new ones to the obj for template types that had not been
created by other cpp.
|
While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created.
| Quote: | Or if it was included in every cpp obj then I guess it would have a flag
in
the obj for to ignore duplicates of the same template type.
|
It's all in the function signatures, I guess.
| Quote: | Just curious how it worked.
|
You're lucky to have time to be curious about those things.
Victor
|
|
| Back to top |
|
 |
Steven C. Guest
|
Posted: Sat Sep 27, 2003 8:20 pm Post subject: Re: templates & obj |
|
|
"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote in
| Quote: | When you compile a cpp program that includes a template class in a .h file
where does the compiled code go for that templated class?
|
Somewhere in the same obj file.
| Quote: | If it went in the
obj for the cpp program seems to me you could end up with multiple
templates
of the same type if the .h file was included in multiple .cpp.
|
Yes, and the linker is supposed to eliminate the multiplicity.
| Quote: | Seems more
straight forward that it would be put in a obj for the .h and the compiler
would just add new ones to the obj for template types that had not been
created by other cpp.
|
While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created.
| Quote: | Or if it was included in every cpp obj then I guess it would have a flag
in
the obj for to ignore duplicates of the same template type.
|
It's all in the function signatures, I guess.
___________________________________________________________
Seem sorta stupid that the compiler is compiling the same code over and over
again, assuming of course the the h file is included in multiple .cpp and
all the cpp require the same types.
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sat Sep 27, 2003 9:20 pm Post subject: Re: templates & obj |
|
|
"Steven C." <nospam (AT) xxx (DOT) com> wrote
| Quote: | "Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote in
When you compile a cpp program that includes a template class in a .h
file
where does the compiled code go for that templated class?
Somewhere in the same obj file.
If it went in the
obj for the cpp program seems to me you could end up with multiple
templates
of the same type if the .h file was included in multiple .cpp.
Yes, and the linker is supposed to eliminate the multiplicity.
Seems more
straight forward that it would be put in a obj for the .h and the
compiler
would just add new ones to the obj for template types that had not been
created by other cpp.
While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created.
Or if it was included in every cpp obj then I guess it would have a flag
in
the obj for to ignore duplicates of the same template type.
It's all in the function signatures, I guess.
___________________________________________________________
Seem sorta stupid that the compiler is compiling the same code over and
over
again,
|
It will compile what you tell it to, as
many times as you tell it.
| Quote: | assuming of course the the h file is included in multiple .cpp and
all the cpp require the same types.
|
Read again what Victor wrote:
"While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created."
The contents of any #included headers become part of the
text of the file which #includes them. This combination
is known as a 'translation unit', which the compiler
translates into an 'object file'. The compiler cannot
know what the contents of other files are, they're
not input to the compile.
-Mike
|
|
| Back to top |
|
 |
Steven C. Guest
|
Posted: Sat Sep 27, 2003 11:00 pm Post subject: Re: templates & obj |
|
|
"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net>
"Steven C." <nospam (AT) xxx (DOT) com> wrote
| Quote: | "Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote in
When you compile a cpp program that includes a template class in a .h
file
where does the compiled code go for that templated class?
Somewhere in the same obj file.
If it went in the
obj for the cpp program seems to me you could end up with multiple
templates
of the same type if the .h file was included in multiple .cpp.
Yes, and the linker is supposed to eliminate the multiplicity.
Seems more
straight forward that it would be put in a obj for the .h and the
compiler
would just add new ones to the obj for template types that had not been
created by other cpp.
While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created.
Or if it was included in every cpp obj then I guess it would have a flag
in
the obj for to ignore duplicates of the same template type.
It's all in the function signatures, I guess.
___________________________________________________________
Seem sorta stupid that the compiler is compiling the same code over and
over
again,
|
It will compile what you tell it to, as
many times as you tell it.
| Quote: | assuming of course the the h file is included in multiple .cpp and
all the cpp require the same types.
|
Read again what Victor wrote:
"While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created."
The contents of any #included headers become part of the
text of the file which #includes them. This combination
is known as a 'translation unit', which the compiler
translates into an 'object file'. The compiler cannot
know what the contents of other files are, they're
not input to the compile.
-Mike
___________________________________________________________
Mike,
Either you are being argumentative or misunderstood my response to Victor.
I'm sure with a little imagination and thought you could see how compilers
could be smarter about how they managed and compiled templates.
Have a good day. :-)
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sun Sep 28, 2003 1:33 am Post subject: Re: templates & obj |
|
|
"Steven C." <nospam (AT) xxx (DOT) com> wrote
| Quote: |
"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net
"Steven C."
news:pimdb.9521$Ak3.4846 (AT) twister (DOT) socal.rr.com...
"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote in
When you compile a cpp program that includes a template class in a .h
file
where does the compiled code go for that templated class?
Somewhere in the same obj file.
If it went in the
obj for the cpp program seems to me you could end up with multiple
templates
of the same type if the .h file was included in multiple .cpp.
Yes, and the linker is supposed to eliminate the multiplicity.
Seems more
straight forward that it would be put in a obj for the .h and the
compiler
would just add new ones to the obj for template types that had not
been
created by other cpp.
While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created.
Or if it was included in every cpp obj then I guess it would have a
flag
in
the obj for to ignore duplicates of the same template type.
It's all in the function signatures, I guess.
___________________________________________________________
Seem sorta stupid that the compiler is compiling the same code over and
over
again,
It will compile what you tell it to, as
many times as you tell it.
assuming of course the the h file is included in multiple .cpp and
all the cpp require the same types.
Read again what Victor wrote:
"While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created."
The contents of any #included headers become part of the
text of the file which #includes them. This combination
is known as a 'translation unit', which the compiler
translates into an 'object file'. The compiler cannot
know what the contents of other files are, they're
not input to the compile.
-Mike
___________________________________________________________
Mike,
Either you are being argumentative
|
Not at all.
| Quote: | or misunderstood my response to Victor.
|
Perhaps. Feel free to clarify.
| Quote: |
I'm sure with a little imagination and thought you could see how compilers
could be smarter about how they managed and compiled templates.
|
What compilers *could* be or do doesn't matter. It's
the language definition that does. And what is it
about template handling that you find not 'smart'
enough?
-Mike
| Quote: |
Have a good day.
|
I will, thanks. :-)
-Mike
|
|
| Back to top |
|
 |
David Rubin Guest
|
Posted: Mon Sep 29, 2003 3:50 pm Post subject: Re: templates & obj |
|
|
Victor Bazarov wrote:
| Quote: |
"Steven C." <nospam (AT) xxx (DOT) com> wrote...
When you compile a cpp program that includes a template class in a .h file
where does the compiled code go for that templated class?
Somewhere in the same obj file.
If it went in the
obj for the cpp program seems to me you could end up with multiple
templates
of the same type if the .h file was included in multiple .cpp.
Yes, and the linker is supposed to eliminate the multiplicity.
|
How does this work if you are linking to an object file which is
compiled with a different compiler than yours? I am under the impression
that there is no standard for naming (i.e., mangled names) in object
files.
/david
--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Mon Sep 29, 2003 5:22 pm Post subject: Re: templates & obj |
|
|
"David Rubin" <bogus_address (AT) nomail (DOT) com> wrote...
| Quote: | [..]
How does this work if you are linking to an object file which is
compiled with a different compiler than yours? I am under the impression
that there is no standard for naming (i.e., mangled names) in object
files.
|
You are correct. And it is not supposed to work. C++ language
standard ensures only source-level compatibility between compilers.
Victor
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|