templates & obj

Discussion in 'C++' started by Steven C., Sep 27, 2003.

  1. Steven C.

    Steven C. Guest

    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.
     
    Steven C., Sep 27, 2003
    #1
    1. Advertising

  2. "Steven C." <> 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.

    > 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.

    > Just curious how it worked.


    You're lucky to have time to be curious about those things.

    Victor
     
    Victor Bazarov, Sep 27, 2003
    #2
    1. Advertising

  3. Steven C.

    Steven C. Guest

    "Victor Bazarov" <> 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, assuming of course the the h file is included in multiple .cpp and
    all the cpp require the same types.
     
    Steven C., Sep 27, 2003
    #3
  4. Steven C.

    Mike Wahler Guest

    "Steven C." <> wrote in message
    news:pimdb.9521$...
    > "Victor Bazarov" <> 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 Wahler, Sep 27, 2003
    #4
  5. Steven C.

    Steven C. Guest

    "Mike Wahler" <>

    "Steven C." <> wrote in message
    news:pimdb.9521$...
    > "Victor Bazarov" <> 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 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. :)
     
    Steven C., Sep 28, 2003
    #5
  6. Steven C.

    Mike Wahler Guest

    "Steven C." <> wrote in message
    news:UDodb.10161$...
    >
    > "Mike Wahler" <>
    >
    > "Steven C." <> wrote in message
    > news:pimdb.9521$...
    > > "Victor Bazarov" <> 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.

    > or misunderstood my response to Victor.


    Perhaps. Feel free to clarify.


    >
    > 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

    >
    > Have a good day. :)


    I will, thanks. :)

    -Mike
     
    Mike Wahler, Sep 28, 2003
    #6
  7. Steven C.

    David Rubin Guest

    Victor Bazarov wrote:
    >
    > "Steven C." <> 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
     
    David Rubin, Sep 29, 2003
    #7
  8. "David Rubin" <> wrote...
    > [..]
    > 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
     
    Victor Bazarov, Sep 29, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    3
    Views:
    15,808
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Phil Endecott

    Obj* ptr = new Obj(X)

    Phil Endecott, Jun 3, 2005, in forum: C++
    Replies:
    5
    Views:
    479
    Mark P
    Jun 3, 2005
  3. Shalabh Chaturvedi
    Replies:
    2
    Views:
    454
    Mike C. Fletcher
    Feb 20, 2004
  4. Matthew Thorley

    How do you convert a string obj to a file obj?

    Matthew Thorley, May 4, 2005, in forum: Python
    Replies:
    7
    Views:
    518
    Peter Otten
    May 4, 2005
  5. alf
    Replies:
    9
    Views:
    423
Loading...

Share This Page